Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS: Cannot read property 'value' of null when getting selected value

What I want to do is get the value of the selected <option> from the <select> elemenet however in my current code, I keep getting an error saying:

cannot read property 'value' of null.

This is very frustrating considering I've tried the methods I found online and yet it still doesn't work properly. Any help would be appreciated, thanks!

import React, { Component } from 'react'

class MyClass extends Component{
    constructor(props){
        super(props);

        this.state = {
            selectedItem: ""
        };
    }

    onChange() {
        var selectedItem = document.getElementById("selectItem").value
        this.setState({selectedItem: selectedItem})
    }

    render(){
        return (
            <div style={{textAlign: "center"}}>
                <select id="selectItem" defaultValue="option1" onChange={this.onChange()}>
                    <option value="option1">option1</option>
                    <option value="option2">option2</option>
                    <option value="option3">option3</option>
                    <option value="option4">option4</option>
                </select>

                <div>{this.state.selectedItem}</div>
            </div>
        )
    }
}

export default MyClass

I would like it to display what option is being selected within the div. If possible I would also like it to show the defaultValue upon loading the page as well.

like image 475
Michael Avatar asked Nov 24 '25 04:11

Michael


1 Answers

The reason this isn't working is because the context of onChange is not that of your MyClass component instance, meaning that when onChange is called, this won't be the corresponding instance of MyClass.

The problem can be resolved by adding this line to your constructor:

constructor(props){
    super(props);

    this.state = {
        selectedItem: ""
    };

    // Add this
    this.onChange = this.onChange.bind(this);
}

Update

One way to load the default value of the <select> dynamically:

class MyClass extends Component{
    constructor(props){
        super(props);

        this.state = {
            selectedItem: ""
        };
    }

    componentDidMount() {

        /* Set initally selected value to say, option3 */
        this.setState({ selectedItem : "option3" })
    }

    onChange() {
        var selectedItem = document.getElementById("selectItem").value
        this.setState({selectedItem: selectedItem})
    }

    render(){
        return (
            <div style={{textAlign: "center"}}>
                {/* 
                Specify the select value which will show as option3 by
                default 
                */}
                <select id="selectItem" value={ this.selectedItem }
                        onChange={this.onChange()}>
                    <option value="option1">option1</option>
                    <option value="option2">option2</option>
                    <option value="option3">option3</option>
                    <option value="option4">option4</option>
                </select>

                <div>{this.state.selectedItem}</div>
            </div>
        )
    }
}
like image 114
Dacre Denny Avatar answered Nov 26 '25 18:11

Dacre Denny