React selecting option with object as attribute value

前端 未结 4 854
时光说笑
时光说笑 2020-12-11 00:29

I have an issue with react when I want to change the selected option. The problem is that the value is an object and I can\'t pass it in option value attribut.

See t

4条回答
  •  渐次进展
    2020-12-11 01:01

    You can make use of index of options

    class Selector extends React.Component {
      contructor(props) {
        super(props);
        this.state = { obj: null }
        this.handleChange = this.handleChange.bind(this)
      }
    
      handleChange(e) {
        this.setState({obj: this.props.listOption[e.target.value].obj})
      }
    
      render() {
        
      }
    }
    

    Moreover, I set obj in state as null in the constructor Is there a right way to do it?

    I depends on your requirement. If you want to show at least one option as selected you can keep that instead of null

提交回复
热议问题