how to create react custom hooks with arguments

React Hooks & Custom Hooks

//  reusable custom hooks

function useVar(type = `A`) {
let var = `var${type}`;
let setVar = `setVar${type}`;
// re-declared bug
const [var, setVar] = useState(0);
useEffect(() => {
const timeout = setTimeout(() => setVar(var + 1), 1000);
return () => clearTimeout(timeout);
}, [var]);
return [var, setVar];
}

OK

// function aruments 

function useVarX(init = 0, time = 1000, name = `varX`) {
const [varX, setVarX] = useState(init);
useEffect(() => {
const timeout = setTimeout(() => setVarX(varX + 1), time);
return () => clearTimeout(timeout);
}, [varX, time]);
return [varX, name, setVarX];
// return [varX, setVarX];
}

https://codesandbox.io/s/react-custom-hooks-with-arguments-2848r

https://2848r.csb.app/

TS bug

This expression is not callable.

Not all constituents of type 'string | number | Dispatch<SetStateAction>' are callable.

Type 'string' has no call signatures.ts(2349)

demos


function App() {
const [varA, setVarA] = useVarA();
const [varB, setVarB] = useVarB();
return (
<span>
Var A: {varA}, Var B: {varB}
</span>
);
} // function useVarA() {
const [varA, setVarA] = useState(0);
useEffect(() => {
const timeout = setTimeout(() => setVarA(varA + 1), 1000);
return () => clearTimeout(timeout);
}, [varA]);
return [varA, setVarA];
} function useVarB() {
const [varB, setVarB] = useState(0);
useEffect(() => {
const timeout = setTimeout(() => setVarB(varB + 2), 2000);
return () => clearTimeout(timeout);
}, [varB]);
return [varB, setVarB];
}

https://medium.com/swlh/useeffect-4-tips-every-developer-should-know-54b188b14d9c

https://blog.bitsrc.io/writing-your-own-custom-hooks-4fbcf77e112e

https://dev.to/wellpaidgeek/how-to-write-custom-hooks-in-react-1ana

https://dev.to/patrixr/react-writing-a-custom-api-hook-l16

refs

https://reactjs.org/docs/hooks-custom.html

https://reactjs.org/docs/hooks-rules.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


how to create react custom hooks with arguments的更多相关文章

  1. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  2. [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?

    问: I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was ...

  3. How could I create a custom windows message?

    [问题] Our project is running on Windows CE 6.0 and is written in C++ . We have some problems with the ...

  4. 深入 Create React App 核心概念

    本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...

  5. Part 13 Create a custom filter in AngularJS

    Custom filter in AngularJS 1. Is a function that returns a function 2. Use the filter function to cr ...

  6. tap news:week5 0.0 create react app

    参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...

  7. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

  8. [Angular] Create a custom validator for reactive forms in Angular

    Also check: directive for form validation User input validation is a core part of creating proper HT ...

  9. [Angular] Create a custom validator for template driven forms in Angular

    User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...

随机推荐

  1. map 传递给函数的代价

    https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/08.1.md map 传递给函数的代价很小:在 32 位机器上占 4 ...

  2. python_3 装饰器之初次见面

    装饰器 定义:本质是函数,(只不过是用来装饰其他函数而已),就是为其他函数添加附加功能 原则: 1. 不能修改被修饰函数的源代码 2.不能修改被修饰函数的调用方式 实现装饰器的知识储备 1.函数即&q ...

  3. 代理模式详解:静态代理+JDK/CGLIB 动态代理实战

    1. 代理模式 代理模式是一种比较好的理解的设计模式.简单来说就是 我们使用代理对象来代替对真实对象(real object)的访问,这样就可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标 ...

  4. java项目相对路径

    ./的含义: eclipse相对路径是相对项目的src目录来说的,而不是相对于当前文件. "./某某文件.txt" 而idea则相对于项目根目录 "./src/某某文件. ...

  5. 改造xxl-job的客户端日志文件生成体系

    为什么要改造XXL-JOB原有的日志文件生成体系   xxl-job原本自己的客户端日志文件生成策略是:一个日志记录就生成一个文件,也就是当数据库存在一条日志logId,对应的客户端就会生成一个文件, ...

  6. MapReduce编程练习(四),统计多个输入文件学生的平均成绩,

    问题描述: 在输入文件中,有多个,其中每个输入文件代表一个学生的各科成绩,其中每行的数据形式为<科目,成绩>,你需要将每个文件中的每科目的成绩进行统计,然后求平均值. 输入文件格式: 这里 ...

  7. 【uva 1610】Party Games(算法效率--构造 dfs)

    题意:有一个N个字符串(N≤1000,N为偶数)的集合,要求找一个长度最短的字符串(可不在集合内)S,使得集合中恰好一半的串小于等于S,另一半大于S.如果有多解,要求输出字典序最小的解. 解法:本来我 ...

  8. acm内容

  9. zjnu1749 PAROVI (数位dp)

    Description The distance between two integers is defined as the sum of the absolute result of subtra ...

  10. 【noi 2.6_9271】奶牛散步(DP)

    这题与前面的"踩方格"重复了,而且是大坑题!题目漏写了取模12345的条件! 详细解析请见我之前的博文--http://www.cnblogs.com/konjak/p/59368 ...