Skip to content

How can I programmatically change the value of a field? #171

Answered by edmundhung
MoSattler asked this question in Q&A
Discussion options

You must be logged in to vote

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>

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@MoSattler
Comment options

@chwizdo
Comment options

Answer selected by MoSattler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants