1

I have a JSON of courthouse that contains one id and one name. The JSON contains an unlimited number of courthouses. I need to create a <Select> full of <Option>. the <Option> value will be the id, and the <Option> content will be the name.

So i got this function

createSelectCourtHouse:function(){
    return ( 
    <Form.Field className='form-control' type='select' name='request.courthouse_id' id='accused_lastname' placeholder="Nom de l'accusé" id='accused_lastname' onChange={this.handleChange.bind(this, 'accused_lastname')}>
       this.state.courthouseData.forEach(function(courthouse) {
           return (
               <option value={courthouse.i}>{courthouse.name}</option>
           );
       })
    </Form.Field>)
},

but it seems like i can't do a forEach inside a return. So i tried this way :

var field = ( 
      <Form.Field className='form-control' type='select' name='request.courthouse_id' id='accused_lastname' placeholder="Nom de l'accusé" id='accused_lastname' onChange={this.handleChange.bind(this, 'accused_lastname')}>
         this.state.courthouseData.forEach(function(courthouse) {
              <option value={courthouse.id}>{courthouse.name}</option>
          });
      </Form.Field>
    )
return field;

But it doesnt work. I feel like im close to getting the answer but simply cant get the hand on it. Any ideas ?

TL;DR : I need to create a select with option filled with value from a JSON.

3
  • forEach doesn't return anything, you probably want to use reduce Commented Feb 25, 2016 at 23:44
  • or more likely map Commented Feb 25, 2016 at 23:45
  • could you be less specific ?? Commented Feb 25, 2016 at 23:54

2 Answers 2

3

You might want to read Why Select Value? in the docs

It's not clear to me why you've created Form.Field abstraction when the select element is perfectly find for expressing your needs

<select className="form-control" name="request.courthouse_id">
  {this.state.courthouseData.map((courthouse, i) => (
    <option key={i} value={courthouse.id}>{courthouse.name}</option>
  ))}
</select>

This has the added benefit of being able to pass a value prop to the select element that will properly display the default/chosen option.

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

2 Comments

Because i'm using the React formal librairie, it eases the further validations.
Nevertheless, the courthouseData.map syntax should help you out
1

Use map to create jsx elements. Try:

var options = this.state.courthouseData.map(function(courthouse) {
   return (
     <option value={courthouse.i}>{courthouse.name}</option>
   );
});

I'm not sure where this function is being called but, chances are you want to return the output in your render function:

render: function() {
  return (
    <SomeParentElement>
      {options}
    </SomeParentElement>
  );
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.