2

Lets say I have a component that maintains the state of some children components. This component holds an update function that contains the logic for update the state. I pass the state and the update function to the children component, that are memoized. The thing is the state is not being updated, or the state is cleared after the last change is made.

My question is how to proceed with this memoization, due to the real one has a lot of textfield to update.

In this sandbox I reproduce a minimum example.

Cheers!

1 Answer 1

1

Try to make your update function memorized with useCallback

const update = useCallback((item, value) => { setData((prevState) => ({ ...prevState, [item]: value, })); }, []); 
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, this works, thanks, but what I really need is to memoize the children component. Although you need to pass data into the dependencies array for this callback
It is possible only with useCallback, because when the children component doesn't take functions as props. Functions are updating after every re-render
with prevState there is no need to pass data as an argument for callback
You know what, your response works charmingly
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.