score:0
your code seems to work. the reason why you don't see the change with a console.log is that setstate
is asynchronous. this function doesn't update the value, it tells react you want to update it, and react will do it by itself very soon after. the consequence is that the state is not reassigned immediately, and your code logs the state before it is updated.
however you can pass a callback as second argument to setstate
which will be called right after the update is done: setstate(newvalue, callback)
.
setblog({...blog,
id: foundblog.id,
title: foundblog.title,
content: foundblog.content,
description: foundblog.description,
publisher: foundblog.publisher,
published: foundblog.published
}, () => console.log("blog", blog)); // this is called after the state
// is effectively updated
Source: stackoverflow.com
Related Query
- set state is not changing the state of an object
- useState - do not rerender the state is set with the same object content
- How can I set up state in a React app using arrays in a json object as well as arrays not in the json object?
- State set method did not effect the main object - ReactJS
- useState hook can only set one object at the time and return the other object of the same array to initial state
- this.state not the same as state in this object
- useState react hook does not set the array state
- useState set method not changing the value - React
- Why does the name of the state object not need to match the name in setState?
- Making an Axios call within a forEach function to ammend an object is not being recognized in the render despite being in the state
- How can I properly set a type for `useState`, if I want the piece of state to either be an object or null?
- React ui not updating although i'm changing the state
- React setState not changing the state
- Why is my hook variable in React not holding the state I set it to?
- Not able to set the JSON object of Arrays in React js using axios.get function and display the object of Arrays in table
- this.setState not updating the state unless I pass the whole object to it
- Not able to set the state properly in react
- React: Looping over an object to bind useState functions is not updating the state of my application
- Redux reducer not changing the store's state properly
- React cannot set state inside useEffect does not update state in the current cycle but updates state in the next cycle. What could be causing this?
- React redux - pushed object in the state is not mapped but initialState is
- Function to set the state of a nested object with argument variable as key
- SetState is not changing the state value
- Set state does not update the dropdown checkboxes - Using hooks
- setState does not set the state properly, when dropdown values changes
- Not able to set the state in the ReactJS context API after AJAX call
- React onChange Method not changing state of object
- Input fields do not respond to changing state to empty object
- What is the best way to set initial state to avoid error: "can not read property of undefined"?
- Why is the state changing on all of my list items and not just one? React.js, State, HandleChange, Modal
More Query from same tag
- How to Focus TextInput in React Native Android
- How to use the jsx conditionals in react?
- Adding custom colors to palette gives Object is possibly 'undefined'. TS2532
- Toggle Single Item in array of data
- How can I include authorization Headers in my react-app?
- Why can't I have updated states in function component (using hook)?
- React export declaration statement expected
- How to show value calculated from input form to the screen
- Switch CSS classes with state
- Uncaught Invariant Violation on rendering google charts using react
- Keeping track of selected div in grid of divs with React state
- TypeError: Cannot read property 'map' of undefined, issue in passing props
- Does not exist on type 'false | HTMLAudioElement'
- Where to put ReactDOM.render in React
- How to use React-redux to avoid local state in my code?
- Performing a mysql query from node/express server using ? placeholder getting syntax error | npm mysql package
- React update view when api's loading variable is false
- How can I persist localstorage when ComponentDidMount resets the value on refresh?
- How to render parts in React
- how to remove duplicating data entry in a row when creating a table with reactjs
- Reactjs - better solution for setState inside fetch
- Updating state with new value to the object of array is really slow
- Trouble getting CORS to work between CodeIgniter and React app
- How often Service Worker refreshes?
- Is there a way to create a ColoredCheckbox component?
- Firebase Analytics Googletagmanager Error
- onChange event triggered twice within ReactJS project
- Reducer not connected to redux state
- How to filter array using single KEY with multiple values?
- Textfield with dropdown material ui items won't show ReactJS