如何将数组用作反应选择组件的选项 [英] How to use an array as option for react select component
本文介绍了如何将数组用作反应选择组件的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想替换选项
<option value="A">Apple</option>
<option value="B">Banana</option>
在给定示例中,通过在React jsx文件中使用数组,我将如何进行?
in given example by use of an array in a react jsx file,how would i proceed ?
<select value="B">
<option value="A">Apple</option>
<option value="B">Banana</option>
</select>
最诚挚的问候
蓝色
推荐答案
因为它只是JavaScript,所以有上百万种方法.我通常采用的方式是映射容器以生成胆量. for循环或其他方法也可以正常工作.
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>;
}
};
或者在es6中,您可以做出更现代的反应
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>;
这篇关于如何将数组用作反应选择组件的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文