2

I have a react component created as a const and with props.

I also have a function see: selectChanged() {} is another file which is returning undefined when I change the select value.

Here is the component code:

... 

const myComponent = (props) => <div id="myid">
    <form onSubmit={props.onSubmit} className="gs-form">

        <div className="label">MySelect</div>
            <select
                id="myselect"
                value=""
                onChange={props.selectChanged}>
                <option value="one" defaultValue>one</option>
                <option value="two">two</option>
                <option value="three">three</option>
            </select>
        </div>
    </form>
</div>;

export default myComponent;

And the data is passed to this:

selectChanged(value) {

    console.info(value);

}

The problem is that it keeps showing undefined what I change the select value.

How can I fix this so that 'selectChanged' passes the value of the selected select ?

1
  • its because of this: value="", either remove it, or use a variable and update that in onChange function, like this: value={props.selectedValue} Commented Nov 23, 2018 at 15:56

1 Answer 1

1
change= (event) => {
         this.props({selectChanged: event.target.value});
     }
...


<select
    id="myselect"
    value={this.props.selectChanged}
    onChange={this.change}>
    <option value="one" defaultValue>one</option>
    <option value="two">two</option
    <option value="three">three</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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