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.

How can I access data from Form outside react-final-form

See original GitHub issue

So, I would like to acess the data from Form outside him, for example, I have multiple forms in only a page:

<Form
  validate={validate}
  onSubmit={ (values, props) => props.reset() }
  render={
  ({handleSubmit, invalid, ...props}) => {
    return(
      <form onSubmit={handleSubmit}>
      ...
      </form>
    )
  }}
</Form>

<Form
  validate={validate}
  onSubmit={ (values, props) => props.reset() }
  render={
  ({handleSubmit, invalid, ...props}) => {
    return(
      <form onSubmit={handleSubmit}>
      ...
      </form>
    )
  }}
</Form>

//And I want check if all forms are valid, for example:
<button disable={ form1.invalid || form2.invalid }></button>

Someone have a idea about that?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:12

github_iconTop GitHub Comments

22reactions
rflmykcommented, Feb 8, 2019

Nice, I did it:

export const myForm = ({subscription, ...props}) => {
	return(
		<Form
			onSubmit={(val)=> console.log(val)}
			subscription={subscription}
			render={({handleSubmit}) => (
				<form onSubmit={handleSubmit}>
				....
				<FormSpy
					subscription={{values: true, valid: true}}
					onChange={(state) => {
						const {values, valid} = state
						props.exposeValues({values, valid})
					}} />
				</form>
			)}
		/>
	)
}

Work very well for me, thanks dude, cya

5reactions
bertho-zerocommented, Nov 2, 2020
// useConstant.js
import { useRef } from 'react';

export default function useConstant(init) {
  const initiated = useRef(false);
  const ref = useRef(undefined);

  if (!initiated.current) {
    initiated.current = true;
    ref.current = init();
  }

  return ref.current;
}
import { createForm } from 'final-form';

function MyComponent() {
  // `form` can also come from props, if needed
  const form = useConstant(() => createForm({
    initialValues,
    onSubmit
  }));

  // use your form...

  return (
    <Form
      form={form}
      render={() => {
        // You're <form />
      }}
    />
  );
}

https://github.com/final-form/react-final-form/blob/92cd23e237b43d1dd8e5598381ab58ae8f97f9ed/src/ReactFinalForm.js#L58-L74

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access react-final-form values from outside the form
The simplest way I found to get the form state outside the form is to use a MutableRefObject to get a reference to...
Read more >
Final Form Docs – FAQ
If you define a variable outside of your form, you can then set the value of that variable to the handleSubmit function that...
Read more >
React Final Form - Accessing other field values - CodeSandbox
React Final Form - Accessing other field values. Example to access the value of other react-final-form fields, to impact form behaviour.
Read more >
How to use React Final Form to Manage Form State
In this tutorial, we will explore React form state management using React Final Form. React Final Form allows us to manage state without...
Read more >
Top 5 react-final-form Code Examples - Snyk
Learn more about how to use react-final-form, based on react-final-form code examples created from the most popular ways it is used in public...
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