0

Can React.useMemo second argument array contains an object?

I ask this question because I have an expensive computation based on an object's value.

I don't know should I expand the object, or just simply pass the object into that array.

1
  • Yes you can but what are the reasons to. How frequent does value for the second object change? If it never changes during lifecycle of the component, it could be better embedded in the object value. I would only pass a second object in the array if I needed to optimise computation on changing values of it. Commented Jun 10, 2019 at 5:30

1 Answer 1

1

It is possible to use an object as 2nd argument. But it depends on how the object behaves. If there will be always a new instance of this object each time the affected value(s) has changed, React.useMemo will be able to detect the change. Since React.useMemo will only do an instance compare in case of an object, it will not detect changes within that object if the instance remain the same. If the instance changes more often then the affected properties, it would be better to extract only the required properties and hand them over individually. This will ensure the calculition will only be done if need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.