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.

Set the value of an input based on something outside of the form?

See original GitHub issue

I looked in the documentation, and didn’t find an answer to this, so I apologize if it’s covered and I missed.

Our app has some fields which are linked to each other. Imagine they look like this:

<input name="input-a" onChange={e => {props.handleChange(e); props.updateParentStuff(e)}} />
<input name="input-b" onChange={e => {props.handleChange(e); props.updateParentStuff(e)}} />

When one of the inputs is updated, the parent hits an API to get some kind of value back, and then the other input needs to be updated, or vis-a-versa. So the flow looks like this:

Update input-a => pass value to parent => call api => return value => update input b
OR
Update input-b => pass value to parent => call api => return value => update input a

The issue is I can’t find a way to force an input in the Formik form to have a certain value, outside of setting the initialValue parameter (we’re using the render prop). I’m hacking around this problem right now by taking a state variable in the parent, and merging it with the values which get submitted when the form is. I’d like to just have the values represent the actual data to be submitted, though.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
prichodkocommented, Nov 6, 2017

Probably you also want to use - https://github.com/jaredpalmer/formik#enablereinitialize-boolean, which is falseby default.

5reactions
ganpatscommented, Mar 9, 2020

setFieldValue is also useful, for e.g.: props.setFieldValue('birth_date', selectedDate);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Submitting a form with an INPUT outside it, and after modiying ...
I have tried submitting the form in a slightly different way as well - by setting the button's onclick event to point to...
Read more >
Values input to fields in a form are not accessible outside
Solved: Hello, I'm building a power app and I want to filter a data source based on the value Selected in a dropdown...
Read more >
<input>: The Input (Form Input) element - HTML
The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide...
Read more >
useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and ... name: 'test' }) // ❌ doesn't create new...
Read more >
How to Use React to Set the Value of an Input | Pluralsight
Using a controlled form input approach, you can maintain the state values as an input for the various form controls.
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