Answer the question
In order to leave comments, you need to log in
React equivalent hooks for ComponentWillMount Ask?
I looked here , but the selected answer doesn't answer the question. I'm looking for a componentWillMount() equivalent to perform logic similar to:
useEffect(() => {
if (condition) {
// do stuff
}
}, []);
Answer the question
In order to leave comments, you need to log in
First, componentWillMount is not recommended for use. Secondly, it will not protect you in any way from an additional render call if it is an asynchronous operation, and it is not needed for synchronous ones.
Feel free to use componentDidMount and useEffect.
And you can fence yourself off from unnecessary calculations, for example, like this:if (!someCondition) return null;
Try
useLayoutEffect(() => {
if (condition) {
// do stuff
}
}, []);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question