Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request for react hooks: useStateRef & useCallbackRef #22773

Closed
badeggg opened this issue Nov 16, 2021 · 1 comment
Closed

feature request for react hooks: useStateRef & useCallbackRef #22773

badeggg opened this issue Nov 16, 2021 · 1 comment

Comments

@badeggg
Copy link

badeggg commented Nov 16, 2021

The problem is that hooks mix the concept of dependence and effect. An effect execution may need the fresh value of a state but doesn't want the changing of the state trigger the execution. So the state has a confusing place to set. Should the state appear in dependency array? Please check code blow:

const [state1, setState1] = useState(5);
const [dependence1, setDependence1] = useState(10);
const [dependence2, setDependence2] = useState(15);
 
useEffect(() => {
    // I want any changing of dependence1 or dependence2 will trigger
    // the code execution here. And code here need fresh value of
    // state1 but I don't want changing of state1 trigger execution.
    console.log(dependence1 + dependence2 + state1);
}, [dependence1, dependence2]);

A solution hooks provided is use reference to 'boxing' the state.

const [state1, setState1] = useState(5);
const [dependence1, setDependence1] = useState(10);
const [dependence2, setDependence2] = useState(15);
 
const state1Ref = useRef(null);
useEffect(() => {
    state1Ref.current = state1;
}, [state1]);
 
useEffect(() => {
    console.log(dependence1 + dependence2 + state1Ref.current);
}, [dependence1, dependence2, state1Ref]);

As I know, the mentioned problem could be encountered very often, so It would be nice that react hooks provide a hook like useStateRef to solve the problem. useStateRef return an array which containing 3 elements, the first two are identical with useState, the last one is a reference of the state, and the reference .current keeps fresh. Like code below:

const [state1, setState1, state1Ref] = useStateRef(5);
const [dependence1, setDependence1] = useState(10);
const [dependence2, setDependence2] = useState(15);
 
 
useEffect(() => {
    console.log(dependence1 + dependence2 + state1Ref.current);
}, [dependence1, dependence2, state1Ref]);

So as useCallbackRef which is more useful I think.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 16, 2021

This has been requested a few times (#22163, #21931) but the best place to discuss it is on an RFC:
reactjs/rfcs#201

Let's close this issue and move the discussion over to the RFC :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants