Comments (12)
You should never be initialising state in componentWillMount
, ever.
You should either use a constructor
or a class property to initialise state (I think when you say "plain method" you mean class property).
from react.
Can you paste an example of what exactly you were doing inside componentWillMount
to initialise state?
Generally speaking, it's bad practice because it's going to result in an unnecessary render cycle, whereas using a constructor
or class property will simply allow the component to mount with the correct state on the first cycle.
I've never used GitHub wikis, but it seems like a good place for tutorials and tips.
from react.
I should be clear though - the state initialisation thing is mostly a concern because it shows incomplete understanding of what React is actually doing, and how you should develop a component-based application. One extra render cycle is basically a non-issue in all practical cases.
from react.
Good luck with the wiki, as always just let me know if you need any help.
from react.
We should remove the comment about componentWillMount
being used in SSR. This is an outdated comment now - people used to use it for SSR, but there are much better options currently.
I don't understand what this has to do with using constructor
or super
though, can you explain that?
from react.
yes for sure in order to initialize a state
we should use componentWillMount
and today it's not there any more, so I saw two different solutions for this:
- we will go back to the
constructor
if we used theconstructor
as the old days (before I knew what react is even 😅) we will use the.bind
method for binding the methods inside of our class component, or in best case we will have to have a very big constructor method to handle it. - other option is to set a plain method inside the class by it self and invoke it in the
state
property it self.
Documentations Update according to the second point I think we should use the constructor instead of the componentWillMount
.
Thanks for noticing me about the SSR Part, I try to will take care of that 🙏
from react.
Oh Nice to know, every day is a school day 💐, because I was doing it 🙃, in the project even.
But why? just in case somebody asked me in the future.
How ever it has been deprecated now, but what about componentDidMount
does it have the same issue?
from react.
One more thing, is it a good Idea to use the wiki
tab for a non-static things such as the easy guides?
https://github.com/Jawhar-B/React/wiki/MobX-&-creat-react-app
Because I think it's a better use of leaving there out, while it's there for this propose.
so in this way we will have all the static parts to work on in collaborate maybe, and the docs we may just make it there for an easy edit follow and detect by maintainers.
from react.
an example of the project hyfer
I was in:
https://github.com/Jawhar-B/hyfer/blob/master/src/App.js#L170
from react.
I will correct the spelling and try to maintain the language grammars there because I didn't study English as people doing usually, and then I will update the wikis here, I will make a note to @gijscor to make it for maintainers only access.
from react.
Thanks for that example. In those kind of cases, where you are checking cookies and doing other things, it would make sense to use the constructor
, not componentWillMount
.
from react.
I appreciate your help, thanks a lot 😃
from react.
Related Issues (8)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.