不断获取React this.setState不是一个函数,尽管已绑定

时间:2018-11-06 00:28:34

标签: reactjs bind setstate

尽管我已经绑定了被调用的函数,但我仍然收到此错误消息。我已经尝试在构造函数this.onChangeEstablishmentSelected = this.onChangeEstablishmentSelected.bind(this)及其外部onChange={this.onChangeEstablishmentSelected.bind(this)}

进行绑定

我不知道是什么原因造成的,因为它没有任何意义,而且我浏览了许多帖子和官方文档,一切对我来说似乎都很到位。

class PatientFormReview extends Component {

state = {
        materializeInitialized: false,
        establishmentId: 0
    };

onChangeEstablishmentSelected(event) {
    this.setState({
        establishmentId: event.target.value
    });
}

renderOptions() {
    if (this.props.auth.establishments.length === 1) {
        this.setState = {
            establishmentId: this.props.auth.establishments[0].id
        };
        return (
            <Fragment>
                <select value={this.state.establishmentId} disabled>
                    <option value={this.props.auth.establishments[0].id}>
                        {this.props.auth.establishments[0].name}
                    </option>
                </select>
                <label>Establecimiento</label>
            </Fragment>
        );
    } else {
        let options = this.props.auth.establishments.map(establishment => (
            <option key={establishment._id} value={establishment._id}>
                {establishment.name}
            </option>
        ));
        return (
            <Fragment>
                <select
                    value={this.state.establishmentId}
                    onChange={this.onChangeEstablishmentSelected.bind(this)}
                >
                    {options}
                </select>
                <label>Establecimiento</label>
            </Fragment>
        );
    }
}

0 个答案:

没有答案