score:1
Accepted answer
you are correct. setstate is async, and you are trying to fetch weather without waiting for that setstate to finish.
<button onclick={() => {
cityprop("london")
setworldcity("london") //<--- will resolve later, useeffect with catch the result.
}>
you can also do this:
<button onclick={() => {
const newcity = "london" //<--- newcity is now the current city. use this inside of this function as worldcity will be stale until this function finishes.
cityprop(newcity)
setworldcity(newcity)
}>
Source: stackoverflow.com
Related Query
- React useState hook updating only after second click
- React Router only updating after second click
- React updating state only after second click
- React Hooks: useState with onClick only updating the SECOND time button is clicked?
- React Hooks: useState with onClick only updating after the 2nd time button is clicked?
- React state isn't updating at first submit, only after second
- React Leaflet Routing Machine: onClick to add Marker after all waypoints are removed fires only after second click
- React does not change state after updating state in usestate hook (in custom function form)
- React query - Why onSubmit only works after the second click
- Changing order of React components only triggers re-render after second click
- OnClick in react portal works only after second click
- Updating Parent Component State from Child Component with UseState React Hook
- React checkbox get (un)checked after the second click
- React useState hook not updating with axios call
- React hook useState not updating with onSubmit
- React Native Fetch works only on second click
- React usestate not updating on first Click Or on First time
- Search results not updating after second search - REACT
- React Hook useState Not Updating UI
- Reading component state just after setting when using useState hook in react
- React hook useState not updating onclick
- How do I fire React useEffect hook only once after state change?
- Updating object with useState Hook after rendering text input
- The state variable returned after using useState react hook shows .map is not a function
- React state variable not accurate after set state in useState hook
- Updating state from an array with react useState hook
- React hook not updating after the API call
- UseState only shows on second click
- React useState hook isn't updating state as desired
- React useState hook - when to use previous state when updating state?
More Query from same tag
- setInterval in UseEffect getting call 2/4 times
- How do I change a an element's style without triggering a re-render in React?
- Can I map within a map like this?
- Fetching Open Graph Metadata with create-react-app
- calling the set function from a useState using the arrow function method reloads too early
- React testing library differences between clicks using userEvent, fireEvent and regular clicks
- React Native Fetch data from API - BUT not render in flat list, just single time
- react-select - How to ensure the default selected items aren't appearing in both dropdowns?
- Is there a way to work with an Object in Object typescript?
- Change in state not affecting other elements
- material ui input type number reset incorrect value
- React Redux router saga material-ui and tabs corresponding to current route
- How to use MaterialUI's Mobile-Stepper in HypserStack?
- How to render React.js into HTML for portfolio
- React onClick function call refreshes app
- SyntaxError: Unexpected end of JSON input in react/express
- Appending Variable to property in Type
- Multiple layouts not working under react router 4 and redux
- Next.js Components: Error: error: Return statement is not allowed here
- Setting state issue in react
- React: Passing down props to functional components
- How to make setState in React synchronous
- React Invalid left-hand side in assignment when checking data values
- Webpack: Taking a long time to build
- 1 component 2 conditional return. How render a collection of children ?
- id from url is shown undefined when used privateroute inside route component
- React debounce search Cannot access 'updateSearchQuery' before initialization
- How are Buttons and Popovers connected when using MUI and ReactJS?
- Ignore flowtype error when using specific pattern
- React map return ID of an object like undefined when it has ID