Using setValue, as mentioned in the article, will force the component to update, which is undesirable in this case. The solution is to pass a function to the first argument of useState:
const [value] = useState(() => computeValue());
This will be initialized when component is mounted and the value will be available before rendering without forcing the component to update.
I already mentioned that solution in the article. My solution will do an extra render, yes. But that's a good thing here, because the component won't be blocked from rendering while the value is being computed.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Using
setValue
, as mentioned in the article, will force the component to update, which is undesirable in this case. The solution is to pass a function to the first argument ofuseState
:This will be initialized when component is mounted and the value will be available before rendering without forcing the component to update.
I already mentioned that solution in the article. My solution will do an extra render, yes. But that's a good thing here, because the component won't be blocked from rendering while the value is being computed.