How can I programmatically change the value of a field? #171
-
Hi! I have a button, that when clicked, it would change / prefill certain inputs. |
Beta Was this translation helpful? Give feedback.
Answered by
edmundhung
Jun 8, 2023
Replies: 1 comment 2 replies
-
Hi @MoSattler, there are 2 approaches you can try: // Approach 1 - Controlled input
const [value, setValue] = useState();
// Just set the state on button click
<input value={value} onChange={event => setValue(event.target.value)} />
<button type="button" onClick={() => setValue('something')}>Action</button>
// Approach 2 - Update the input value with useInputEvent
// This will also emit a change / input event and trigger validation based on your form's configuration.
const [ref, control] = useInputEvent();
<input ref={ref} />
<button type="button" onClick={() => control.change('something')}>Action</button> |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
MoSattler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @MoSattler, there are 2 approaches you can try: