简体   繁体   中英

Redux-form, how to set the Field name to a dynamic string?

Redux-form is not allowing me to set the Field name to an integer as it requires a string... How can I set the redux-form Field name to something the API can then use to record the user's option selected per Talent.

        <Field
          name='talent_id[' + {field.talent_id} + ']'
          component={renderField}
          field={field}
          />

This is not working, I'm getting Syntax error: Unexpected token with the +

Talents.rb

id | Title
1 | Jumping
2 | Skipping
3 | Running

My API, returns Talents#Index like so:

[
 {"id":1,"title":"Jumping"},
 {"id":2,"title":"Skipping Rope"},
 {"id":3,"title":"Running"},
 {"id":4,"title":"Something Else"}
]

I then amusing the above to build a form that can later be submitted to the server.

<div>
   <label>Jumping</label>
   <select name="1">
      <option></option>
      <option value="1">XXX</option>
      <option value="2">YYY</option>
      <option value="3">ZZZ</option>
   </select>
</div>
<div>
   <label>Skipping Rope</label>
   <select name="2">
      <option></option>
      <option value="1">XXX</option>
      <option value="2">YYY</option>
      <option value="3">ZZZ</option>
   </select>
</div>
....

If you are use using ES5, simply like this:

  <Field
      name={`talent_id[${field.talent_id}]`}
      component={renderField}
      field={field}
  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM