componentdidmount is for side effects. adding event listeners, ajax, mutating the dom, etc.
componentwillmount is rarely useful; especially if you care about server side rendering (adding event listeners causes errors and leaks, and lots of other stuff that can go wrong).
there is talk about removing
componentwillmount from class components since it serves the same purpose as the constructor. it will remain on
according to documentation setting the state in
componentwillmount will no trigger a re-rendering.
if the ajax call is not blocking and you return a
promise that update the component's state on success, there are chances that the response arrives once the component has been rendered.
componentwillmount doesn't trigger a re-render you won't have the behaviour you expected which is the component being rendered with the requested data.
if you use any of the flux libraries and the data requested end up in the store the component is connected to (or inherit from a connected component) this won't be an issue as the reception of that data will, most likely, change props eventually.
i had the same issue at the beginning, too. i decided to give a try making requests in
componentwillmount but it end up in various small issues.
i was triggering rendering when ajax call finishes with new data. at some point rendering of component took more time than getting response from server and at this point ajax callback was triggering render on unmounted component. this is kind of edge case but there is probably more, so it's safer to stick to
- Why do the React docs recommend doing AJAX in componentDidMount, not componentWillMount?
- Why does calling react setState method not mutate the state immediately?
- Why is `Promise.then` called twice in a React component but not the console.log?
- Why ajax request should be done in componentDidMount in React components?
- Why does my Animated view not stay in the correct place in React Native?
- Why is clearTimeout not clearing the timeout in this react component?
- Ajax is not setting the headers - React with TS
- Why is my React checkbox onChange handler firing on render and then not when the box is clicked?
- Why is componentDidMount the best place to fire AJAX request
- React + Redux - Why not connect all the components?
- Why does react call render function if I have not changed the reference of the state?
- React componentDidMount not updating the state
- Why is this variable from the Redux store not in the `this.props` on React page?
- What is the benefit of having $sce or Strict Contextual Escaping in angularjs and why react does not need to do it?
- Why does my React app append the external URL to http://localhost:/<port> and not to ONLY the URL itself?
- Why is react state(array) empty inside callback function? Why is it not using the updated value from outer scope?
- Why is the state not updating inside this react Hooks component?
- Why is my react Table not updating the data?
- Why CSS file is not loaded in the react app
- Why does setState Hook from React is rendering twice even not changing the state?
- In react class component why is the state not being changed in the first instance of button click? But in the second instance it gets updated?
- Why the UI component of the react is not getting updated?
- Why does the jest mock function not getting executed in my test with react testing library?
- Why is my hook variable in React not holding the state I set it to?
- Why can NGINX not load my react app with the right css & js files
- Why does my react app go to the custom 404 not found page when the link is clicked?
- Why is jest not able to provide the useTranslation hook to a custom React hook I'm testing?
- why does it not update the sql row in the react web application?
- Why is my text not changing as I'm writing text to the input tag in React
- Why React component is not update when the props passed to it changes?
More Query from same tag
- AmCharts4 disable radar chart resize/zoom
- How do I pass the props from this child component to the parent form
- Getting 'dispatch is not defined' in redux component
- Reactstrap - collapse Navbar on NavLink click only on mobile
- Pass same Props to multiple React component
- AudioWorklet error: DOMException: The user aborted a request
- Using require('cors') but still getting CORS error
- Slideshow transitions stop working after the first slide
- Dynamic import style and set scss variable value
- React form submit and "can't perform state update on an unmounted component"?
- Unexpected token when using multiple punctuations
- Force re-render the react component in the jquery single page application or in static html site?
- React - File uploader button doesn't work properly
- Different wrap text between horizontal and vertical
- React and Leaflet
- retrieving data from firestore using uid react
- How can I protect a route in React router
- React - how to use redirect inside hook useEffect?
- Using SVG in a JSON file as a file instead of code
- How to delete data from server and UI using React and Node?
- onClick like button likes all objects in array React JS
- React-Bootstrap grid contents not displaying
- react- cleaning autocomplete generates an error
- I have problem using preload function to load images with react-p5 library
- Specifying different momentJS locales per React component
- ReduxToolkit - createAsyncTask Get Data after Post
- Update nested object's property in Redux
- React-draggable npm package prevents clicking inside input field
- How to draw a circle with n slices
- How do I call a function within another function when the data I need is in the global scope?