reactjs – React : is it legit to keep state initial value in useRef?
I came across a piece of code today:
useEffect(() => {
// do some dispatch, etc
}, [dispatch, staticData, route]); // to follow eslint react's exhaustive-deps rule
In this case, the references of dispatch, staticData never change.
But the reference to route can change because we replace the query params in the URL on inputs change.
Solution 1: I can disable eslint react exhaustive-deps for this line. But I’m not confortable with it -> if someone edit this useEffect in the future, I want this person to be aware of this rule.
Solution 2:
const initialRoute = useRef(route); // I could also use const [initialRoute] = useState(route);
useEffect(() => {
// now I have to use initialRoute.current to do stuff
}, [dispatch, staticData, initialRoute]);
It does the job and eslint is fine with it.
But I wonder: is there a better way to store an initial value than useRef?
Read more here: Source link
