question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read state onSubmit

See original GitHub issue

I expect that after action(data) the state would be updated. But it remains the previous state. How do I access the updated state?

const onSubmit = (data) => {
    action(data)
    console.log('data:', data) // partial store
    console.log('state:', state) // previous state
  }

This is invoked using RHF: <form onSubmit={handleSubmit(onSubmit)}>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ptimcommented, Jul 18, 2020

Really appreciating all the examples 👍

@kevinsproles Here’s the where the useEffect would go in the example CSB from the docs for WizardFormFunnel: https://codesandbox.io/s/form-wizard-pages-kkg7m?file=/src/Result.js:153-204

Something like:

useEffect(() => {
    // post to backend here
    console.info(state);
    // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
0reactions
raffibesikcommented, Jan 18, 2021

I’ve encountered this issue as well but I believe I have figured out a work around though by simply using the Object.assign() function to the current state with the data from the current step/page/component.

const {actions, state} = useStateMachine({updateAction});
const onSubmit = async (data) => {
    actions.updateAction(data);
    Object.assign(state.yourDetails, data);
    console.log(state.yourDetails);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating state of component when onSubmit and display on ...
It works import React from "react"; import "./styles.css"; class App extends React.Component { constructor(props) { super(); this.state ...
Read more >
Forms - React
In React, mutable state is typically kept in the state property of components, ... Because its value is read-only, it is an uncontrolled...
Read more >
How to Get All Form Values on Submit in React - Webtips
Learn how you can properly grab all form data at once in React using a state object with useState.
Read more >
Get input values on Form submit in React | bobbyhadz
To get input values on form submit in React, store the values of the input fields in state variables. Set the `onSubmit` prop...
Read more >
FAQs | React Hook Form - Simple React forms validation
React Hook Form is focusing on uncontrolled inputs, which means you don't need to change the input value via state via onChange ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found