Now with hooks, I've got the component's state split into more miniature states. This seemed better until I wanted to find out what state change caused a specific re-render.
How can one easily find out what state change caused a specific re-render?
Now with hooks, I've got the component's state split into more miniature states. This seemed better until I wanted to find out what state change caused a specific re-render.
How can one easily find out what state change caused a specific re-render?
If state updates need to be tracked for state management like accessing previous state values, this should be handled explicitly with custom hooks that wrap useState
.
As for debugging, React dev tools currently don't offer this functionality, although this information may be available through renderer undocumented API used in dev tools.
It's possible to add a breakpoint inside React dispatchAction
function and walk call stack up to know which state setter was called:
Or custom hook can be used instead of useState
to keep track of state updates:
const useDebuggableState = initialState => {
const [state, setState] = useState(initialState);
useMemo(() => {
'a line to add breakpoint';
}, [state]);
return [state, setState];
};
dispatchAction
helped me see the function call triggering the infinite rerenders. –
Desensitize © 2022 - 2024 — McMap. All rights reserved.
componentWillUpdate
(orcomponentDidUpdate
) style checks where you have current and future (or previous) state? – VervecomponentDidUpdate
, you useuseEffect
with the dependencies given in an array as second argument so that the effect runs automatically when the dependencies change. You could create auseEffect
for each piece of state you have. – Bricole