Как получить индекс выбранного <option>внутри <select>в React? - PullRequest
0 голосов
/ 20 октября 2019

Я хочу получить индекс выбранного <option> из <select>. Я пробовал jQuery, получая атрибуты объекта напрямую (что показывает, что _this2.selector.current не определено) и selectedIndex, но ни один из них не работал.

<div className="cell-content">
    <select
        id="common-name-selector"
        className="form-control"
        onChange={() => {
            this.handleChange();
            presentSpecies = this.selector.target.value;
        }}
        ref={this.selector}
    >
        {(this.selector.current = 0)}
        <option value="" selected>
            Select by Common Name
        </option>
        {
            data.data.species.map((option, index) => {
                return (
                    <option
                        value={option.index}
                        key={index}
                    >
                        {option.commonName}
                    </option>
                );
        })}
    </select>

    {
        //This is the part I'm having trouble with.
        setInterval(() => {
            console.log(this.selector.current);
            // presentSpecies = 0;
            presentSpecies = this.selector.current.querySelector;
            ReactDOM.render(
                <Router />
                document.getElementById("root")
            );
        }, 500);
    }
</div>
...