1

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>
            );

4
  • where is value property in <option> tag? Commented May 11, 2018 at 11:08
  • add value property to <option>. 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? Commented May 11, 2018 at 11:12
  • You'll able to select any option. To save the selected option you need value Commented May 11, 2018 at 11:15

2 Answers 2

3

You mean this?:

<option key={planet.id} value={planet.id} >{planet.id}-{planet.name}</option>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

<option key={planet.id} value={planet.id}>{planet.name}</option>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.