Prevent rerender on function prop update
- Use React feature
React.memo
for functional components to prevent re-render if props not changed, similarly to PureComponent for class components. - When you pass callback like that:
<Section
...
updateSection={value => updateSection(idx, value)}
/>
your component Section
will rerender each time when parent component rerender, even if other props are not changed and you use React.memo
. Because your callback will re-create each time when parent component renders. You should wrap your callback in useCallback
hook.
- Using
useState
is not a good decision if you need to store complex object likeinitialForm
. It is better to useuseReducer
;
Here you could see working solution: https://codesandbox.io/s/o10p05m2vz