score:0
it is just simple works for me, i do not see the problem. also the callback logs the items.
const loaditems = () => (
new promise(resolve => {
settimeout(() => resolve(['item1', 'item2']), 1500);
})
)
class app extends react.component {
state = { items: [] };
componentdidmount(){
loaditems().then(items => this.additemstostate(items))
}
additemstostate = items => {
this.setstate({ items }, console.log(items))
}
render() {
const { items } = this.state;
return <ul>{items.map(item => <li>{item}</li>)}</ul>
}
}
reactdom.render(<app />, document.getelementbyid('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
score:0
the issue was related to the console log itself. i was using google chrome and seems that the printed console.log json object's preview was referring to the initial state before loading. with an empty array:
{ itemtypes: array(0)}
i haven't tried to expand this preview because array(0) i'd expect to see nothing inside.
instead by expanding the wanted payload response is there even though array(0) was declared.
tricky and confusing.
Source: stackoverflow.com
Related Query
- Why is setState not working in React functional component
- Why is setState is not working as expected
- Why is react setState not working inside useEffect?
- setState not working as expected in react hook
- Why React SetState is not working as expected?
- Why does calling react setState method not mutate the state immediately?
- React setState not working on first try, but works on second?
- Why setCustomValidity() not working in React app?
- fireEvent.keyDown not working as expected on my Jest + React Testing Library test
- setState not working for updating an array in React
- why if-else condition is not working while using in react jsx
- Why ReactHighcharts SolidGauge's rounded is not working in react
- sequential setState calls not working as expected
- setState not working in componentDidMount() - React
- React onLoad img event not working as expected
- Replace of setState callback in react hook with useEffect hooks for complicated scenario not working
- Why is TestUtils.Simulate.click in Jest not working when used directly on React Components?
- why destroyOnClose={true} not working in React
- <picture /> element is not working as expected in Safari browser, while using React
- React Ionic (v5): IonReactRouter & IonRouterOutlet Not Working as Expected
- React Redux is not working as expected with Next.js & NodeJS
- React - setState on input field not working
- React useEffect() not working for Logout as expected
- React css webkit animation is not working after setState
- React router after adding createBrowserHistory, the app is not working as expected
- React setState hook not working with useEffect
- why activeClassName property in NavLink is not working in 6.0.2 version of react router?
- setState callback not working as expected
- Why is SVG textPath not working in Firefox when rendered by React
- React form event.preventDefault() not working if handler calls setState on sibling component
More Query from same tag
- Is that possible to next line in Input tag in html css?
- Cannot find react component
- Setting new state can change the data's color attribute, but not image
- Dont render Search component with Router React js
- Cannot get property 'ndkVersion' on extra properties extension as it does not exist with... react-native 0.64.0
- Use typescript to limit payload type with action name
- How to navigate between screens in react without usage of libraries like react router
- Displaying images with img tag in React.js
- Get ref from connected redux component withStyles
- React Redux - Methods on store objects
- Why I got Error while Creating React App?
- React get element from child after render
- I just try to check what happens componentdidmount change state and value after render calls but get unexpected result
- Using Semantic UI With CSS Modules in Webpack
- Blank screen in reackhooks Home page with Maximum update depth exceeded. This can happen when a component calls setState
- How to change icon colour when hovering on enclosing div container
- Infinite loading with react-table + react-virtualized / react-window
- React JS: Component Not Exported
- Not able to figure out why docker compose up is not running my React JS App
- why link doesn't work when I use getStaticProps?
- Getting filename from React fetch call
- Check for duplicate in array before adding to react state
- React: Target container is not a DOM element
- Style material-ui tooltip using @emotion/styled
- Custom style of React-tabs
- Error on mapping an array of objects in react application
- HTML isn't rendered before after translate3d transition
- Reactjs how to change the state of a component from a different component
- Changing a deeper property of an object shows in console but not when we open it or access it later
- ReactJS - Route works but doesn't display the page