score:1
You need to call fetchAccountDetails
right after changing the state, for the function to invoke using the latest state that the dropdown has changed:
dropdownChange = (event: React.SyntheticEvent<HTMLElement>, data:any) => {
this.setState(prevState => ({
selectedAccount: data.value
}), () => { this.fetchAccountDetails() });
}
score:0
the setState
function is asynchronous, which means you need to take special precaution when updating state and expecting to use the updated value in the state synchronously. For this reason, the setState
function has a second parameter which allows you to specify a callback which is executed when the state has actually been updated. This is where you would need to call the fetchAccountDetails
function.
You can find the setState
callback parameter described in the React docs here.
But you will have trouble compiling this either way. (a) Your interface should end each member declaration with a semicolon, not a comma. (b) You're missing a ")" on each line with a call to fetch
. (c) In the fetchAccountDetails
function you declare the URL
variable, but then you pass a string 'URL' to the fetch
function, not the variable.
Here is a working code sandbox showing the fix to your question, and the syntax fixes. I've commented out the calls to the fetch
function since they will fail anyway.
Source: stackoverflow.com
Related Query
- setState does not set the state properly, when dropdown values changes
- React leaflet center attribute does not change when the center state changes
- ReactJS - SetState does not update the state when the state variable is a number
- Set state does not update the dropdown checkboxes - Using hooks
- React does not re-render component when the state changes
- Why does React rerender when the state is set to the same value the first time via an onClick event on DOM, but not react-native?
- React Hook error in array handing in useEffect() and setState does not set the state
- React does not render children when the parent state changes
- Why does calling react setState method not mutate the state immediately?
- ReactJS: Why are the constructor called when setState changes the state
- React setState does not set state during a while loop
- loading indicator does not show when the page route changes
- Material UI TextField label does not move up when the value for TextField is set porgramatically
- useEffect does not re-run when the params changes
- useState react hook does not set the array state
- React Hooks state not working properly when the function calling it is called in another function
- React.js component not re-rendering children when the useState hook changes state
- HighCharts not updating when the state changes in React
- useState does not set state when initalized by RouteProps
- Im trying to build a menu that has collapsible options, but the dom does not update even when state data updates
- React class does not update when state changes
- Not able to set the state properly in react
- React: When does not the count value changes in setInterval?
- React jsx array component does not re-render when state changes
- Reach child does not update when state changes
- Re-render does not occur when state changes
- 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 test passes but component does not work properly when state updated in document.addListener handler
- Input element's value in react component is not getting re-rendered when the state changes
- React does not re-render when state changes
More Query from same tag
- How to solve this exception Unexpected token < in JSON at position 0?
- How to format React Codes in Visual Studio Code?
- ReactJS: State only updates every second click
- pass key to object to update nested property value
- Save parameters into LocalStorage and Redirect React
- How can I use dynamic Menu Component in next.js
- React.PropType with two possible shapes
- Authentication with oidc-client.js and Identityserver4 in a React frontend
- Adding html and js code under React src folder
- Time Slider in Javascript
- Exclude .js files from build React.js
- Need to add different styling based on props being passed
- React: When passing in a function to one instance of a component, why are all other instances effected?
- Is it bad practice to return a JSX element from a React hook?
- ReactJS : use state is not working for only one state
- Unable to read data from Redux store in the React app
- Seeing unknown typescript error in JSConfig.json file
- How to check if music is playing with Last FM API
- React pdf downloads blank
- How to integrate Reselect in my React + Redux application?
- React - styled components constructor from variable
- React.js how to I go about getting data from my form and posting it
- React.js infinite re-render
- Material-UI for react, how layout Card component to align texts
- How to Read image data from API and render in React Component
- Material UI AppBar with React Router 4 Implementation not working
- Switch animation in Expansion panel not working
- Find a file in React
- Update array of data using useState hook in React
- Request/add webcam after calling navigator.mediaDevices.getUserMedia() or removing video track