A very common scenario in const [watchValue, setWatchValue] = useState(''); const [otherValue1, setOtherValue1] = useState(''); const [otherValue2, setOtherValue2] = useState(''); useEffect(() => { doSomething(otherValue1, otherValue2); }, [watchValue, otherValue1, otherValue2]); We want to execute Here comes a troubling question:
This problem can be solved by changing const [watchValue, setWatchValue] = useState(''); const other1 = useRef(''); const other2 = useRef(''); // ref does not need to be added to the dependency array because the reference remains unchanged useEffect(() => { doSomething(other1.current, other2.current); }, [watchValue]); In this way, the variable references of This is a headache in You can combine the features of import { useState, useRef } from "react"; // Use the reference trait of useRef while maintaining the responsiveness of useState type StateRefObj<T> = { _state: T; value: T; }; export default function useStateRef<T>( initialState: T | (() => T) ): StateRefObj<T> { // Initialization value const [init] = useState(() => { if (typeof initialState === "function") { return (initialState as () => T)(); } return initialState; }); // Set a state to trigger component rendering const [, setState] = useState(init); // When reading value, the latest value is obtained // When setting value, setState will be triggered and component rendering const [ref] = useState<StateRefObj<T>>(() => { return { _state: init, set value(v: T) { this._state = v; setState(v); }, get value() { return this._state; }, }; }); // What is returned is a reference variable, which remains unchanged during the entire component life cycle return ref; } So, we can use it like this: const watch = useStateRef(''); const other1 = useStateRef(''); const other2 = useStateRef(''); // Change the value like this: watch.value = "new"; useEffect(() => { doSomething(other1.value, other2.value); // Actually, these three values are now reference variables, which remain unchanged during the entire component life cycle, so there is no need to add dependency arrays. // However, the eslint plugin of react hooks can only recognize useRef as a reference. If it is not added, it will warn people. For the safety of variable references, it is still added. }, [watch.value, other1, other2]); In this way, The above is the details of the React tips on how to get rid of the troubles of hooks dependency. For more information about React hooks dependency, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Installation tutorial of mysql 5.7 under CentOS 7
Prerequisite: The web developer plugin has been in...
Table of contents 1. Use the "rpm -ivh insta...
Table of contents 1. Concept 2. Environmental Des...
1. Install Docker First open the Linux environmen...
We all know that after the MySQL database is inst...
Preface As we all know, JavaScript is single-thre...
Table of contents 1. Prototype mode Example 1 Exa...
mysql accidentally deleted data Using the delete ...
There are many Hadoop installation tutorials on L...
When using the font-family property in CSS to ref...
Lambda Expressions Lambda expressions, also known...
This article records the detailed installation tu...
This is the effect to be achieved: You can see th...
To do a paginated query: 1. For MySQL, it is not ...
Table of contents Preface 1. The request content ...