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. WPF TabControl美化

    <Window.Resources> <!-- TabItem的样式 --> <Style TargetType="{x:Type TabItem}" ...

  2. elasticsearch从开始到永久

    0.学习目标 独立安装Elasticsearch 会使用Rest的API操作索引 会使用Rest的API查询数据 会使用Rest的API聚合数据 掌握Spring Data Elasticsearch ...

  3. CF976B

    这是一道考验思维找规律的题,很有可做性. 正文 题意 一个 n * m 的矩阵,从左上角(1 , 1) 开始,先向下走直到最下方,再向右走到最右,再向上走一个,再走到最左......一直走到(1 , ...

  4. Java——集合框架之Set&HashSet,HashMap,泛型,compareTo

    Set Set接口--数据存放无序,非常简单,主要呈现信息列表 Set接口存储一组唯一.无序的对象 HashSet是Set接口常用的实现类 Set接口不存在get方法 Iterator接口:表示对集合 ...

  5. eclipse中Tomcat修改项目名称

    1.打开你的项目目录,找到一个.project文件,打开后修改<name> test</name>中的值,将test修改成你要修改的名字: 2.在项目目录下,打开.settin ...

  6. snmp协议 及snmpwalk

    推荐阅读: snmp及工具:https://www.jianshu.com/p/dc2dc0222940 snmp协议详解:https://blog.csdn.net/shanzhizi/articl ...

  7. css按钮样式

    style='height:22px;padding:1 17px;font-size: 8px;font-weight: 100;line-height: 25px;'http://www.boot ...

  8. PHP-文件、目录相关操作

    PHP-文件.目录相关操作 一  目录操作(Directory 函数允许获得关于目录及其内容的信息) 相关函数: 函数 描述 chdir() 改变当前的目录. chroot() 改变根目录. clos ...

  9. TypeScript 入门教程学习笔记

    TypeScript 入门教程学习笔记 1. 数据类型定义 类型 实例 说明 Number let num: number = 1; 基本类型 String let myName: string = ...

  10. POJ - 3693 Maximum repetition substring(重复次数最多的连续重复子串)

    传送门:POJ - 3693   题意:给你一个字符串,求重复次数最多的连续重复子串,如果有一样的,取字典序小的字符串. 题解: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现 ...