React selecting option with object as attribute value

前端 未结 4 850
时光说笑
时光说笑 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 00:47

    I assume you want only one option will be selected. So the easiest way would be to set selectedIndex. When using construct always think of value type. this.state = { selectedIndex: 0}

    Now you've state with selectedIndex object which firstly is equal to 0.

    In render method you could then just check for the index:

    {this.props.listOption.map((option, index) => {
        this.state.selectedIndex == index ? (
          
        ): (
          
        ))}
    

    And on handle change setState with e.target.key.

    I may have left syntax errors... Altought I hope it helps.

提交回复
热议问题