score:1
I agree with Kenji's comment... You have to do that in ComponentDidMount...
https://reactjs.org/docs/state-and-lifecycle.html#adding-lifecycle-methods-to-a-class
and if you want to show a loading message you can set loading state and use that condition in render function like...
constructor(){
this.state={loading:true}
}
ComponentDidMount(){
//do your async thing and change the state to loading:false
}
render(){
if(this.state.loading)
return <YourMessage />
return <Something />
}
Source: stackoverflow.com
Related Query
- React : trouble with setState in componentWillMount
- react component renders twice with only one setState in componentWillMount
- Updating an object with setState in React
- Prevent react component from rendering twice when using redux with componentWillMount
- setInterval with setState in React
- Trouble with Fetch in React with CORS
- updating objects with setState in React
- react setState with dynamic key
- Trouble with simple example of React Hooks useCallback
- Changing style property with setState in React
- what happens in react when setState with object instance of a class
- typescript react setState with user input
- React Context - setState with onClick inside Consumer
- Multiple fetch requests with setState in React
- React use debounce with setState
- React with typescript - type safety with setState
- React scroll animation with setState is choppy
- setState with Firebase Promise in React
- React not updating state with setState with Array of object
- React setState with promise as a callback
- Material ui Textfield Hinttext overlap with with text when text is set with setState in react
- Replace of setState callback in react hook with useEffect hooks for complicated scenario not working
- React js single function with multiple setstate
- React state, setState with const
- React - useEffect with async setState causing additional re-renders
- How setState works with memo in react
- Real world usage of setState with an updater callback instead of passing an object in React JS
- React setState hook not working with useEffect
- React hooks call setState with same primitive value still cause re-render
- concatenation setState with dynamic key in react using es6
More Query from same tag
- How to pass props to child components when using protected routes?
- React web app: 'Hide' elements of a table so those with value of zero are not shown
- react-hook-form: handleSubmit is not a function
- Transpiled code gives TypeError:is not a constructor
- How to avoid this message warning "Maximum update depth exceeded..." on NextJs
- reactjs setState not changing state
- Redux state change does not rerenders datatable
- React - How to pass returned data from an exported function to a component?
- React Intl- Cannot read property 'formatMessage' of undefined
- Styled Components: Style Parent if child has attribute
- Max-height and max-width for divs with dynamic backgrounds?
- Refused to execute script from 'bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled
- Bootstrap columns not responsive in webpage
- react-chart-js-2 keep bar width no matter what the number of bar is
- React open full picture onClick
- How do I store data for particular user only
- Using UNSAFE_componentWillMount in strict mode is not recommended and may indicate bugs in your code
- Inline styles and ReactCSSTransitionGroup
- Call react js function from html file
- Objects are not valid as a React child, render a collection of children, use an array instead
- How to use usestate and setstate instead of this.setState using react and typescript?
- React Day Picker, avoid adding modifier styles for outside days of month
- Typescript throws an explicit function return type error when using Suspense with React Router
- How to stop a script completely when its tag is removed
- How to stop execution of a function which is triggered by a button click using another button in ReactJS?
- How do I test result of dispatching async action creator in redux?
- Redux is executing the wrong action
- React, this.setState not updating a variable
- React how to send object to onClick event
- Callback in Custom React Hooks