score:0
you should be using the setstate
callback i.e a correct way to handle state mutations since setstate
takes time to mutate the state and hence any use of state immediately after updating the state may result in the older value and not the updated value.
make use of callback like
cityhandleupdateinput() {
this.setstate({
city: this.refs.city.refs.searchtextfield.input.value
}, () => {
const { city } = this.state;
this.props.fetchrescity(city)
})
}
make sure that you are binding the cityhandleupdateinput
function in constructor
or from where it is being called or otherwise it itself won't be able to access the setstate
with this.setstate
score:1
setstate
won't immediately update this.state
. here is the explanation from the react docs:
setstate()
does not immediately mutatethis.state
but creates a pending state transition. accessingthis.state
after calling this method can potentially return the existing value.there is no guarantee of synchronous operation of calls to
setstate
and calls may be batched for performance gains.
that means you'd need to store the value you are passing to setstate
, and pass that to your action creator as well. something like this (i didn't run this code, make sure you test it) should work:
cityhandleupdateinput() {
const city = this.refs.city.refs.searchtextfield.input.value;
this.setstate({ city });
this.props.fetchrescity(city);
}
Source: stackoverflow.com
Related Query
- How can ı pass function with state for react useContext
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How can I redirect to another component in react and pass the state that I set in the previous component?
- How can i pass a state from one react route to another react route?
- How can I pass calculated amount to state in react
- How can I pass entire react state with post request
- How can I pass arguments in action different 3 level react components without redux and context API?
- How to update my react state with active tab menu name so it can pass on to the endpoint call?
- How I can pass the state and action to Redux in functional component?
- How can I pass state to an action in React.js?
- How can I pass props and state interface to a React class if I extend a custom class?
- React - how to pass state to another component
- How can I remove an attribute from a React component's state object
- React router and this.props.children - how to pass state to this.props.children
- How to pass state values to initial values in formik in react js
- Can you pass down state to child components with React Hooks?
- How to pass a state className variable to another component in react
- How can I pass a variable from 'outside' to a react app?
- How can I set initial React state from API data?
- How can I pass data using <Redirect> in react router v4?
- using css modules in react how can I pass a className as a prop to a component
- Can not pass state with react router dom v6 beta, state is null
- How can I pass data from express server to react views?
- How can I copy the state in react without reference?
- How do I pass all state variables inside react hooks to my child component
- ReactJS - How can I implement pagination for react with keep state when route change?
- How can I pass CSS classes to React children and ensure they will override the child’s local class?
- How can mock the value of a state and data in my react test
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How can i dispatch an action from a helper function in react native
More Query from same tag
- Why can't I call my Redux action creators from my event handler function but I can from componentWillMount()?
- timezone conversion using date-fns
- Is there a way where I could put a line break for each data it displays?
- fecth id and date from url as pros in react js
- reactjs ,jsx iterate over nested object
- Helmet expects a string as a child of <head>. Did you forget to wrap your children in braces
- Upload and render image in draft-js based editor
- Can you create a react component in JSX where the component comes from a passed down property?
- can't resolve stripe - import axios from axios NEXTJS
- Lodash filter matching for multiple values in nested objects
- How to convert private key to base64 format?
- Matreial ui is not loading Styles and Theme?
- Unable to access/manipulate data from weather API using axios with useEffect in React.js
- websocket.js causing unexpected refresh of React app
- ReactJS : Writting a style in createStyle method
- implementing infinite loop of changing text with React Hooks
- Function component keeps rerendering over and over
- How to handle conditional thunk calls depending on state
- React.js Log out did not turn into log in after I log out in nav bar
- Get HTML from page outside of react comnponent
- How do I stop nested React components that dispatch Redux actions which update state from getting stuck in an infinite loop?
- Any efficient way to render nested json list in React?
- React Typescript Class Component Default Props Interface
- TypeScript doesn't see defaultProps for functional components
- React Js / Menu with menu item drop down and menu item active State
- How to mock a class exported from a third party library?
- Cypress react cy.visit() any url that is not index
- ReactJS: How to call a parent's setState() from this.props.children?
- material ui chip not working inside a loop
- Getting null when accessing to HTMLcollection