score:72
componentWillMount is essentially the constructor. You can set instance properties that don't affect render, pull data from a store synchronously and setState with it, and other simple side effect free code you need to run when setting up your component.
It's rarely needed, and not at all with ES6 classes.
score:0
ComponentDidMount()
Method only changes the current page in class components but ComponentWillMount()
changes all the pages which effected by setStates()
score:1
Use-case for the componentWillMount()
For example, if you want to keep the date of when the component was created in your component state, you could set this up in this method. Please keep in mind that setting state in this method won’t re-render DOM. This is important to keep in mind, because in most cases whenever we change the component’s state, a re-render is triggered.
componentWillMount() {
this.setState({ todayDate: new Date(Date.now())});
}
Use-case for the componentDidMount()
For example, if you were building a news app that fetches data on the current news and displays it to the user and you may want this data to be updated every hour without the user having to refresh the page.
componentDidMount() {
this.interval = setInterval(this.fetchNews, 3600000);
}
score:2
componentWillMount https://daveceddia.com/where-fetch-data-componentwillmount-vs-componentdidmount/
There’s a “gotcha,” though: An asynchronous call to fetch data will not return before the render happens. This means the component will render with empty data at least once.
There is no way to “pause” rendering to wait for data to arrive. You cannot return a promise from componentWillMount or wrangle in a setTimeout somehow.
our Component will not have access to the Native UI (DOM, etc.). We also will not have access to the children refs, because they are not created yet. The componentWillMount() is a chance for us to handle configuration, update our state, and in general prepare for the first render. This means we can start performing calculations or processes based on the prop values.
score:9
As per the documentation ( https://facebook.github.io/react/docs/react-component.html )
Methods prefixed with will are called right before something happens and
Methods prefixed with did are called right after something happens.
score:34
componentWillMount
is done before the INITIAL render
of a component, and is used to assess props and do any extra logic based upon them (usually to also update state), and as such can be performed on the server in order to get the first server side rendered markup.
componentDidMount
is performed AFTER the initial render
when the DOM has been updated (but crucially BEFORE this DOM update is painted to the browser, allowing you to do all kinds of advanced interactions with the DOM itself). This of course can only happen in the browser itself and so does not occur as part of SSR, as the server can only generate markup and not the DOM itself, this is done after it gets sent to the browser if using SSR
Advanced interactions with the DOM you say? Whaaaat??... Yep - at this point because the DOM has been updated (but the user has not seen the update in browser yet) it is possible to intercept actual painting to the screen by using window.requestAnimationFrame
and then do things like measure the actual DOM elements that will be output, to which you can perform further state changes, super useful for example animating to a height of an element that has unknown variable length contents (as you can now measure the contents and assign a height to the animation), or to avoid flash of content scenarios during some state change.
Be very careful though to guard state changes in any componentDid...
as otherwise can cause an infinite loop because a state change will also cause a re-render, and hence another componentDid...
and on and on and on
score:38
To add to what FakeRainBrigand said, componentWillMount
gets called when rendering React on the server and on the client, but componentDidMount
is only called on the client.
score:64
the constructor
method is not the same as componentWillMount
.
According to the author of Redux, it is risky to dispatch actions from the constructor because it may result in mutating the state while rendering.
However, dispatching from componentWillMount
is just fine.
from github issue:
This happens when dispatch() inside one component's constructor causes a setState() inside another component. React keeps track of the “current owner” for such warnings—and it thinks we're calling setState() inside the constructor when technically constructor causes a setState() inside some other part of the application. I don't think we should handle this—it's just React trying its best do its job. The solution is, as you correctly noted, to dispatch() inside componentWillMount() instead.
Source: stackoverflow.com
Related Query
- What is the difference between componentWillMount and componentDidMount in ReactJS?
- What is the difference between React Native and React?
- What is the difference between state and props in React?
- What is the difference between HashRouter and BrowserRouter in React?
- What is the difference between .ts and .tsx extensions. Both are used as extensions for typescript files in react. So where should we use them?
- What is the difference between redux-thunk and redux-promise?
- What is the difference between NextJs and Create React App
- What is the difference between jest.fn() and jest.spyOn() methods in jest?
- What is the technical difference between .jsx and .js files
- What is the difference between hashHistory and browserHistory in react router?
- What is the difference between Reactjs and Rxjs?
- What is the difference between .env.local and .env.development.local?
- What is the difference between Jest and enzyme?
- What is the difference between JavaScript and JSX?
- What is the difference between `PropTypes.node` and `PropTypes.any` in react?
- What is the difference between render and return in reactjs?
- What is the difference between Redux Thunk and Redux Saga?
- what is the difference between import and const and which is preferred in commonjs
- What is the difference between Box and Grid in Material-UI?
- What is the difference between @material-ui and @mui
- What is the difference between .js, .tsx and .jsx in React?
- What is the difference between useQuery and useLazyQuery in Apollo graphQL?
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- react-redux: What is the difference between state.setIn() and state.set()?
- In React, what is the difference between onKeyUp and onKeyUpCapture (and onKeyDown/Capture)?
- What is the main difference between using React-Redux Hooks and React-Redux Connect()?
- What is the main difference between React Query and Redux?
- what is the difference between ( )=>React.FC and ( )=>JSX.Element
- what is the difference between React.HTMLProps<> and React.HTMLAttributes<T>?
- What is the difference between "Drop Down Menu" and "Select Field" in Material-UI
More Query from same tag
- Textcontent not updating in react
- React context api update state instantly
- React test checkbox doesn't checked on click
- React Native - How to deal with asynchronism with AsyncStorage
- React state, remove item in array after a while
- Redux: display single record but json is an array
- How to keep images in an HTML template inside Outlook from expanding unnecessarily with React?
- Visual Studio (2017 & 2019) ignoring TypeScriptCompileBlocked
- babel transpilation returns object instead of string or function
- react native firebase how to make query synchronous
- Return value from saga to react component
- Why is typescript complaining that the variable can be undefined when my function checks that it's not undefined?
- type for material UI icons
- When I wrap the index.js with my context provider I just see a blank page
- React.forwardRef Causes Error with Styled Component
- React - zIndex does not work
- useState, setting icon active and inactive
- How to test correct state update in async componentDidMount axios call?
- best way to handle fetching Status in redux
- Swiperjs in React : swiper.slideTo TypeScript
- Unable to read map property of an array
- How to add/use React component in only one part (cart page) of my existing node project?
- Why is my state update overwriting existing data values?
- AWS Coginto API , Forgot Password Action not raising InvalidParameterException even if neither a verified phone number nor a verified email exists
- How to solve async search options delay?
- How to update cart quantity if item already exist in Cart - reactjs?
- How can i set toggle my switch correctly on Ant Table List?
- React Higher Order Component not rendering passed component
- How to call SCSS 'parent selector reference &' to react css module?
- pass dynamic title in page of NextJS Head in exported static website