Git Product home page Git Product logo

Comments (2)

sudheerj avatar sudheerj commented on July 16, 2024 1

Thanks for the details @AnandKumarJha and @KateDK. This question didn't say that "It is not safe to use setState() inside componentWillMount" but talks about not recommended to use in async initialization in componentWillMount(). That's why I mentioned componentDidMount instead of componentWillMount.

I updated the answer in the first sentence as "Yes, it is safe to use setState() in componentWillMount" to avoid confusion. Please let me know for any concerns.

from reactjs-interview-questions.

KateDK avatar KateDK commented on July 16, 2024

Few points:

  • Perhaps its a good idea to mention that this method will be deprecated eventually and is not recommended to be used?

  • Im not sure the current answer is wrong, but I think it answers the wrong question.

Using setState inside componentWillMount() is fine:
"The componentWillMount() is a chance for us to handle configuration, update our state, and in general prepare for the first render. At this point, props and initial state are defined. We can safely query this.props and this.state, knowing with certainty they are the current values. This means we can start performing calculations or processes based on the prop values."
taken from here.
"calling setState() synchronously in componentWillMount() method will not trigger an extra rendering"
taken from here

However: It is not recommended to use asynchronous actions inside componentWillMount():
"These lifecycle methods have often been misunderstood and subtly misused; furthermore, we anticipate that their potential misuse may be more problematic with async rendering."
taken from here

another great explanation:
"Well, when you do an async request in the constructor / componentWillMount you do it before render gets called, by the time
the async operation has finished the render method most probably already finished and no point to set the "initial state" at this stage is it?.
I'm not sure this is your case here, but most of the cases that developers wants to initiate state asynchronously in componentWillMount is to avoid a second render call. but you can't avoid it can you, like mentioned above, render will fire anyway before the async operation will finish.
So, the best time to call an async operation is after a render has called and the component mounted (you could mount null or an empty <div/>) and then fetch your data, set the state and make it re-render respectively."
taken from here.

In short, calling setState is safe and OK from componentWillMount(). However, it is not recommended to do subscriptions or asynchronous actions as those can cause additional rendering.
I think the current answer code should be updated to actually use componentWillMount() (currently its componentDidMount()) and in the code block display an example that does not use asynchronous actions- as those are not recommended.

I hope this was helpful.

from reactjs-interview-questions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.