How to use an array as option for react select component

后端 未结 2 1462
半阙折子戏
半阙折子戏 2020-12-05 04:52

If i wanna replace the options



in given exam

相关标签:
2条回答
  • 2020-12-05 05:13

    Because it's just javascript, there are a million ways. The way I usually take is to map the container to generate the guts. A for loop or whatever would also work just fine.

    const Answer = react.createClass({
    
        render: function() {
    
            var Data     = ['this', 'example', 'isnt', 'funny'],
                MakeItem = function(X) {
                    return <option>{X}</option>;
                };
    
    
            return <select>{Data.map(MakeItem)}</select>;
    
        }
    
    };
    

    Or in es6 in more modern react you can just

    var Answer = props => 
        <select>{props.data.map((x,y) => <option key={y}>{x}</option>)}</select>;
    
    0 讨论(0)
  • 2020-12-05 05:23

    You're usually required to supply a unique key to make sure React can identify items, you can use uuid to do this or a key you have on hand, e.g.

      <Select name={field}>
        {FBButtons.map(fbb =>
          <option key={fbb.key} value={fbb.key}>{fbb.value}</option>
        )};
      </Select>
    
    0 讨论(0)
提交回复
热议问题