score:1
Accepted answer
with the below code block, you are setting the state and immediately you are trying to access the updated value, but state updates done in async fashion. you will get the latest value in the next re-render.
...
setstarttime(moment(timethestart).format("yyyy-mm-dd"))
setendtime(moment(timetheend).format("yyyy-mm-dd"))
// these 2 logs prints initial value, not updated value.
console.log(starttime);
console.log(endtime)
...
you can use a useeffect
to log or do something with latest values of starttime and endtime.
useeffect(() => {
console.log("starttime", starttime);
console.log("endtime", endtime);
}, [starttime, endtime]);
Source: stackoverflow.com
Related Query
- ReactJS useState not updating momentJS prop
- useState not updating directly in reactjs
- ReactJs UseState : insert element into array not updating
- useState is not updating inside array.map(). ReactJS
- Reactjs useState hook not updating on promise data
- useState is not updating in ReactJS
- Pomodoro Timer REACTJS not working - useState not updating
- reactjs not updating socketio state properly in useState
- useSelector not updating when store has changed in Reducer. ReactJS Redux
- variable in useState not updating in useEffect callback
- React hooks useState not updating with onChange
- Video displayed in ReactJS component not updating
- ReactJs PWA not updating on iOS
- key is not available as prop in the Child component in ReactJs
- ReactJS component textarea not updating on state change
- setState is not updating state: ReactJS
- useState hook not updating a value
- Updating initialValues prop on Formik Form does not update input value
- Reactjs prop available in render but not in componentDidMount
- React useState hook not updating with axios call
- Label text not updating in MUIDataTable ReactJS
- React hook useState not updating with onSubmit
- React usestate not updating on first Click Or on First time
- setState() not updating the View - reactjs
- ReactJS component state is not updating when any value in state array is modified
- React Hook useState Not Updating UI
- useState setter not updating state when called in useEffect
- Method not getting correct useState value despite updating state in React
- React: useState filter array not updating state
- Recompose - mapProps and withProps not updating prop
More Query from same tag
- In my react shouldComponentUpdate function,this.props and nextprops have all the same attributes, but they are not equal
- More generic way to set an objects value within React handleChange
- How does React implement its two-way data binding?
- How would I make it so that props.match.params.id can be accessed to any of the children Components?
- Mock-axios-adapter not mocking get request
- ReactJS Typescript pass props in Link react-router-dom
- React: setting state and default input from prop
- Binding element 'x' implicitly has an 'any' type
- How to solve issue in jest unit test?
- Mock inner axios.create()
- React.js: Click on answer option toggles the color of all options instead of just the clicked option
- why react-bootstrap is not adding styles to my grid layout
- styled-components is saying wrapped styled() around your React component (Component)
- Dropdown doesn't work in a loop but normal
- How can i hardcode the date in this function using next js
- React boostrap table`s responsive not working
- How to keep scroll position using flatlist when navigating back in react native ?
- How to check ReactDOM.render method to be called with a react component argument?
- onClick of a btn i want to get it's sibling(input box) value with useRef and copyToClipboard, setState for that btn to Copied
- clearInterval does not reset
- Unable to reset the state of redux while refreshing the listing
- Unable to display a html response inside a div element
- React-Emotion: pass down defaultProps to in component reuse
- yarn install error:Failed to download Chromium
- react webpack scss closures
- Having issues with React LinkedStateMixin
- How to display new text on every blink using CSS
- right way to POST data to a server and handle response with redux
- reach-router background-image not displaying correctly sometimes
- How to pass a html template through props to a react component?