Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React selecting option with object as attribute value

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 the following code:

class Selector extends React.Component {   contructor(props) {     super(props)     this.state = { obj: null }     this.handleChange = this.handleChange.bind(this)   }    handleChange(e) {     this.setState({obj: e.target.value})   }    render() {     <select onChange={handleChange}>      {this.props.listOption.map((option, index) =>         <option key={index} value={option.obj}>          {option.name}        </option>      )}     </select>   } } 

and with

<Selector option={[{name: "name", obj:{...}}, ...]}> 

I need to change the state of the component with the value of the selected option. What I get when the state change is "object Object". I suppose this happens because react can't embed javascript object in attribut of final view. I am right?

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

like image 792
Frilox Avatar asked Mar 02 '17 19:03

Frilox


People also ask

Can select option value object?

To add select option with object as the value attribute value with React, we can set the value attribute to the index and then select the object from the index from the options array. We have an array of options that we render as options in the select drop down. To do the rendering, we call options.

How do I get the selected option value in React select?

Getting the Selected Value To fetch the selected value from the select element, you can use the onChange event handler prop. Just like the input or textarea elements, you can use the onChange event handler to get the value from the event object.

Can you use an object as a key in React?

Objects cannot be used as keys. React js requires a key to be a string or a number and should be unique.


2 Answers

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() {     <select onChange={handleChange}>      {this.props.listOption.map((option, index) =>        <option key={index} value={index}>         {option.name}        </option>       )}     </select>   } } 

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

like image 115
Jyothi Babu Araja Avatar answered Oct 16 '22 08:10

Jyothi Babu Araja


Convert the object to JSON string, and pass it as value.

And convert the string back to object in the handler.

handleChange(event) {     let obj = JSON.parse(event.target.value); //object   }    render() {     <select onChange={handleChange}>      {this.props.listOption.map((option, index) =>         <option key={index}           value={JSON.stringify(option)}> //pass object string as value          {option.name}        </option>      )}     </select>   } 
like image 20
Sreeragh A R Avatar answered Oct 16 '22 09:10

Sreeragh A R