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.

Form onValidate as a prop

See original GitHub issue

Hi, thank you for the great library. I have run into an issue and could use some help. If there’s an intended way for me to do this, let me know and I can close the issue.

🚀 Feature request

<Form> should have onValidate as a prop, not part of state

Motivation

I would like to change the value of onValidate in the form. Consider if onValidate called a callback prop of the enclosing component. This callback will not get updated in the onValidate function because it was only set at the first render’s useFormState() call. There doesn’t seem to be a way to update this value when props change – again, let me know I’m missing something!

Example

The issue is illustrated here. When the parent’s onValidate prop is updated, the Form will still use the old onValidate:

const MyComponent = ({ onValidate }) => { // takes callback as a prop
  const form = useFormState({ // initialize form state
    values: { foo: "" },
    onValidate // will not be updated when parent onValidate prop changes (!)
  });
  return (
    <Form {...form}>
      <FormInput {...form} name="foo" />
    </Form>
  );
};

const Example = () => {
  const [count, setCount] = useState(0);
  return (
    <>
      <Button onClick={() => setCount(count + 1)}>{count}</Button>
      <MyComponent
        onValidate={() => {
          // Note this always prints 0, even though count is changing!
          console.log(count);
        }}
      />
    </>
  );
};

Possible implementations

Could be made similar to the onSubmit prop – which also needs to receive values.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tom-shermancommented, Jul 18, 2019

@cbush I believe the onSubmit prop is attached to the native submit DOM event, that’s why it works as a prop.

1reaction
diegohazcommented, Jul 17, 2019

I’ll need more time to think on this, but one thing you can do today is to use a ref to store the updated value:

function useLiveRef(value) {
  const ref = React.useRef(value);
  React.useEffect(() => {
    ref.current = value;
  });
  return ref;
}

function Component() {
  const [items, setItems] = React.useState([]);
  const itemsRef = useLiveRef(items);
  const form = useFormState({ onValidate: values => itemsRef.current... })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Form onValidate as a prop · Issue #400 - GitHub
I would like to change the value of onValidate in the form. Consider if onValidate called a callback prop of the enclosing component....
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >
How to pass custom props to validate function in redux-form?
I came across this Scenario where I needed values from state and validate the redux form data with them. Solution that I implemented...
Read more >
Final Form Docs – `FieldProps`
These are props that you pass to <Field/> . You must provide one of the ways to render: ... An array of field...
Read more >
useForm | React Hook Form - Simple React forms validation
The values props will react to changes and update the form values, which is useful when your form needs to be updated by...
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