score:6
There are a few issues, one or more of which are probably the problem (particularly #4):
State changes are asynchronous. You won't see the changes to state on
this.state
immediately after calling it. If you need to see the changes, use the callback (the second argument tosetState
).setState
has no return value, so it doesn't make sense to callsetState
with the return value ofsetState
. I wouldn't think that was the problem, but this:this.setState(this.setState(res.data));
should be
this.setState(res.data);
The documentation doesn't say that the
render
method can returnundefined
. So it's not defined (no pun!) if you returnundefined
, as you're doing.In your
render
, you're usingthis.provider
. Ifprovider
is provided byres.data
, you should be usingthis.state.provider
.
You mentioned looking at various other methods (componentShouldUpdate
, etc.), but there's no need in this case. You're kicking off your async process in the right place (componentDidMount
), and setting state in response to that completing (which is also correct, though see #1 and #2 above).
score:-1
Please see below.
import React, { Component } from 'react';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
class FigosTest extends React.Component {
state = {
provider: 'unknown',
date: 'sometime'
};
componentDidMount() {
axios.get('x.com/some_api')
.then(res => {
console.log(res.data.provider);
console.log(res.data.date);
this.setState(res.data, () => {
console.log("After");
console.log(this.state.provider);
console.log(this.state.date);
});
});
}
render() {
return this.provider || 'undefined';
}
}
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Result: <FigosTest/>
</p>
</header>
</div>
);
}
}
export default App;
score:1
You can also confirm if your state has updated or not.
this.setState(res.data, () => console.log(this.state));
Source: stackoverflow.com
Related Query
- React doesn't update component after state changes
- How to update React component after changing state through redux?
- How to test state update and component rerender after async call in react
- How does React update a component and its children after a state change?
- react router v6 won't switch to Home Page after Login Page: Warning: Can't perform a React state update on an unmounted component
- Warning: Can't perform a React state update on an unmounted component after update context and redirect to another page
- How to update state after React Router changes automatically?
- Functional Component does not rerender after state changes [ React ]
- Can't get React component to update after setting state after Axios call
- How to update items in a component after state changes
- React useEffect doesn't reload my component after state update
- React update state of component after search through object
- React + Redux - Component doesn't update after state change
- react and redux, update state from store changes in a component
- React - Update state in class component based on text inputted by user after button click
- React how to update ui after every loop iteration which changes state array?
- REACT + FIRESTORE : Listen to changes in firebase collection in a react component to automatically update the react state
- React children component not re-rendering after parent state changes
- What react lifecycle component to use to update state after an axios post
- React doesn't update component state when user changes input
- React functional component stops rendering on state changes after remounting
- React doesn't re-render component after state changes when main thread is queued up with other operations?
- State of React child doesn't update after updating parent component
- React does not re-render component after state update
- Redux react not re-rendering component after state changes
- react component does not rerender after update state
- React - state doesn't update in class component even though the message printed on the screen changes
- React not re-rendering component after page refresh, even if the state changes
- Test react component after state changes
- Can't perform a React state update on an unmounted component after API fetch
More Query from same tag
- How can I increment the value of my reaction text by 1 when clicked
- How to get mui Chip value before delete on Autocomplete freeSolo?
- Filter array by property and other array
- How to call action in react before the first load of page?
- Unable to access Redux state in React component
- React: how to test if method is called when updating props?
- renderToNodeStream [object Object] server-side-rendering
- React hooks event listener re-added on each render (exhaustive-deps error)
- "Actions must be plain objects. Use custom middleware for async actions." while using redux-thunk
- setState() or markNeedsBuild() called during build. in Navigator Push
- NextJS wrong CSS order on production build
- Setting options of a select option from API
- radio input is getting frozen in reactjs
- find consecutive operators in a string and replace previous operator with latest operator
- SVG logos not showing up in Chrome
- How to properly set up material-ui with electron-react-boilerplate
- How to change useState in functional component (child) from class component (parent)
- TypeError: Cannot read property 'group' of undefined
- How can I assign a variable the promise results of an async function?
- Counter going above abviable stock and below initial values
- Redux - Specify which reducer from action.type when using combineReducer
- React Icon component with SVG's
- React JS - Change color on click and put defaul color on all other ones
- Open Outlook web client or in browser from Outlook compose deeplink
- React MUI: How to create a flex-box like grid using the Grid component
- Fetching data from firebase realtime database returns invalid json response
- why scroll down navigation bar is not changing the color?
- Is it possible to change the rendered output of a component by wrapping it in another component?
- Getting json object data with react
- Validating an input that do not suppose to allow decimals, it accepts 0.0