admin管理员组文章数量:1425896
I have a Select
that get's values from API,
then on change it gets the value from the select and renders a table. but I want the select to display planet.id
and planet.name
, but doing it using event.target.value
wont work since it would pass both values and table wouldn't render!
Any way of doing this passing for example key={planet.id}
?
class Planet extends React.Component {
constructor(props) {
super(props);
this.state = {value: 1};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event){
this.setState({value: event.target.value});
}
render () {
let planets = this.props.state.planets;
let optionItems = planets.map((planet) =>
<option key={planet.id} >{planet.id}</option>
);
I have a Select
that get's values from API,
then on change it gets the value from the select and renders a table. but I want the select to display planet.id
and planet.name
, but doing it using event.target.value
wont work since it would pass both values and table wouldn't render!
Any way of doing this passing for example key={planet.id}
?
class Planet extends React.Component {
constructor(props) {
super(props);
this.state = {value: 1};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event){
this.setState({value: event.target.value});
}
render () {
let planets = this.props.state.planets;
let optionItems = planets.map((planet) =>
<option key={planet.id} >{planet.id}</option>
);
Share
Improve this question
edited Oct 10, 2019 at 9:17
Vadim Kotov
8,2848 gold badges50 silver badges63 bronze badges
asked May 11, 2018 at 11:04
zemmsoareszemmsoares
3531 gold badge8 silver badges28 bronze badges
4
-
where is
value
property in<option>
tag? – Raviteja Commented May 11, 2018 at 11:08 -
add
value
property to<option>
. – Amanshu Kataria Commented May 11, 2018 at 11:09 - @Raviteja after adding a value={planet.id} made it work, but strangely it was working without the value property? – zemmsoares Commented May 11, 2018 at 11:12
-
You'll able to select any option. To save the selected option you need
value
– Raviteja Commented May 11, 2018 at 11:15
2 Answers
Reset to default 3You mean this?:
<option key={planet.id} value={planet.id} >{planet.id}-{planet.name}</option>
Try this
<option key={planet.id} value={planet.id}>{planet.name}</option>
本文标签: javascriptReactJS Select KeyStack Overflow
版权声明:本文标题:javascript - ReactJS Select Key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745416206a2657696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论