Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Final Form. How to set field value from component state

Tags:

I'm trying to add a date picker to my forum. Problem is the date picker only works through the component state. meaning that the picked values go into the state.

I'm trying to force a final form field to use that state value. But I can't seem to figure it out.

Basically trying to to this:

<Field name="field1" value={this.state.date}/>
like image 998
jdstrong95 Avatar asked Feb 22 '19 15:02

jdstrong95


1 Answers

Ummmmm, I'm going to assume you wan't to set an initial value that the end user can change. Please let me know if this is not the case.

If you wan't to force a value, don't use a field. React-final-form maintains state so you don't have to.

To set an initial value, use initialValues property in the Form control.

 <Form
      onSubmit={onSubmit}
      initialValues={{ field1: "2019-02-02" }}
      render={({ handleSubmit, form, submitting, pristine, values }) => (
        <form onSubmit={handleSubmit}>
          <div>
            <label>Feild One</label>
            <Field name="field1" component="input" type="date" />
          </div>
          .
          .
          .

Edit 🏁 React Final Form - Simple Example

like image 150
Mke Spa Guy Avatar answered Sep 27 '22 18:09

Mke Spa Guy