0

render form to input values:

return (

handleSubmit will check if the form is valid and ready to be submit, it call the callback function // that is in our case, the onSubmit()

    The name property specifies what piece of state is being edited
                  <Field
                      label="Email"
                      type="email"
                      name="Email"
                      component={this.renderField}
                  />

            <Field name="city" 
                label="Cidade"
                value={this.state.value}
                onChange={this.handleChange}
                options={options}                                  
                component="select">               
                        </Field>

                  <Field
                      label="Password"
                      type="password"
                      name="password"
                      component={this.renderField}
                  />
                  <button type="submit" className="btn btn-primary">Submit</button>
                  <Link to="/register" className="btn btn-danger">Already registered?</Link>
              </form>
          );

I'm getting the values from json but the field it's empty

7
  • Doing res.data gets an array of city elements Commented Dec 28, 2017 at 10:59
  • Now the question is, what is options. Commented Dec 28, 2017 at 11:31
  • Maybe you need to render selectbox as soon as this.state.cities is populated. Commented Dec 28, 2017 at 11:32
  • @lumio options it's the let options variable, i want to pass the values to the inputs of select options, can you guide me please? Commented Dec 28, 2017 at 11:35
  • Add your full render function Commented Dec 28, 2017 at 11:37

1 Answer 1

2

Your initial state might not be set, so this.state.cities is actually empty.

Do something like this in your render function:

render() {
  const options = ( this.state.cities || [] ).map( (city) => ( {
      value: city.name,
      label: city.id
  } );

  return (
    -- other components --

    { options.length ? (
        <Field name="city"
            label="Cidade"
            value={this.state.value}
            onChange={this.handleChange}
            options={options}
            component="select"
        />
      ) : null }

    -- other components --
  );
}

( this.state.cities || [] ) will check if this.state.cities is available, otherwise it uses an empty array.


A little more detail: Your axios call is asynchronous. That means, that React doesn't wait for axios to fetch your data but instead just tries to render something.

Your state has not been set (probably) and therefore you get this error.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks it's now working, the problem it's that the select options dont show in the form. Can you please help? @lumio
Is your console.log working GetCitys().then? Please update your question then
It's getting all data from json but don't get the values in the select input @lumio
It is null until it has been loaded. React rerenders your component as soon as your state changes.
Not working the field render for select @lumio, after wait for the answer the render dont show the values.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.