score:2
Accepted answer
No you can't pass txnId
to props.history.push
just after call setTxnId
. This because setTxnId
is async. Use data.txnId
instead. So your code becomes:
const handleSubmit = async (e) => {
e.preventDefault();
const { data } = await axios.post(
"https://api",
{
mobile: vmobile,
}
);
setTxnId(data.txnId);
if (data.txnId) { // <-- here use data.txnId instead of txnId
props.history.push({
pathname: "/authorise",
state: { txnId: data.txnId }, // <-- here pass data.txnId instead of txnId
});
}
}
Otherwise, if you want to use txnId
just updated, you should use useEffect
hook in this way:
useEffect(() => {
if (txnId) {
props.history.push({
pathname: "/authorise",
state: { txnId: txnId },
});
}
}, [txnId]);
const handleSubmit = async (e) => {
e.preventDefault();
const { data } = await axios.post(
"https://api",
{
mobile: vmobile,
}
);
setTxnId(data.txnId);
}
Source: stackoverflow.com
Related Query
- React state gets updated only after I submit the form twice
- State gets Updated only when we click on the button twice and not once by using Recat Hooks
- the state inside hooks are not updated for first time on form submit in react
- REACT - auto submit form after files uploaded to state the contains array
- Authenticity token only gets set after form submission in React component which throws an error in Rails
- Why it gets the correct data from localStorage only after refreshing the page in React
- React Hooks only update state on form submit
- React prop only changes state after selecting twice
- How can I make react render the new state modifications after I have updated the state with hooks and context
- React JS: previousState in setState is not keeping the data of the previous state after the state is updated
- How to update the React State only after a return from API called is completed
- Why won't the form state visibly change within the submit button using react form hook?
- React form not displaying the correct inputs on the screen after I click submit button
- React setState doesn't update the state after submitting input form
- React - Clear form after submit with Context API state management
- How to change the checkbox selection and get the updated id's of checkbox items in form submit in React Component?
- React state having select not updating once the form gets submitted
- after updating the state, component is called twice in react-redux, which leads to getting initial state again instead of updated state
- Doesn't re-render after button submit in react hooks if state updated
- How to reset the react-select component to defaultValue state after form submit
- React not triggering re-rendering of form inputs after state is updated via onChange
- After submit the form how do I redirect to welcome component in React
- Access the updated value after Set state in React
- How to Remove the form value After Submit in React from below code?
- How can we hold/persist the selected dropdown value after submit and reset the textarea in react hooks form
- Reseting react bootstrap's form after the submit
- TypeError: Cannot assign to read only property 'value' of object react after using the value of state in a function
- How to submit a form with radio buttons in react and increment the state by one based on the value submitted?
- React onChange doesn't return updated value after updating the state
- How to set the initial state value after fetching data to update a form in react
More Query from same tag
- Adding items to an array in javascript
- How to skip the update of state inside getDerivedStatefromProps() in childComponent only for the first time of rendering the childComponent
- Strange javascript toString() behaviour
- Add Avatar (user profile photo) to Semantic UI React's Dropdown
- React-native android WebView handle clicked Url before loading
- in redux-react on initial component render dispatched and state is changed
- How to use onClick event to pass the props from child component to parent component React Hooks
- Calling React component method of redux-form
- Material UI 1.0 beta, accent color raised button with white text
- React: what are use cases when we need React.Children api
- Stop function is not working as expected. Need Help. React
- Load transalatons from backend and use with react-i18next
- TypeError: Cannot read property of undefined react redux
- Arguments instead of Props in React Functional Component
- React Router Dom redirect from redux action creator
- Correct way to set state after redux action
- Get all child component hrefs using a React higher-order component
- React Native ScrollView scrollTo function is not working
- Calling Child component function from Parent component in React.js
- Updating state while in a JSZIP file
- How to change background color in react materialUI card
- React.js TypeError this.props is undefined
- Error: Rendered more hooks than during the previous render. How to fix?
- Component does not rerender in react when changing tab item
- Render JSX from material-ui Button onClick
- Why is my home page component rendering on every route?
- Probleme to reload a ReactLeaflet map
- ERESOLVE unable to resolve dependency tree while installing a pacakge
- TypeError: Cannot read property 'focus' of undefined in ReactJS
- contacts.map is not a function