【发布时间】:2020-03-05 12:00:31
【问题描述】:
我正在使用 react-bootstrap-typehead 构建打字头并遇到 TypeError。
详情: 我可以成功地输入字段、过滤选项并使用菜单,但是,在选择一个选项后,页面变为空白并显示此错误:
“TypeError:无法读取未定义的属性 'onChangeState'”
160 | <Typeahead
161 | id="basic-typeahead-example"
162 | labelKey="state"
> 163 | onChange={(e) => this.onChangeState(e)}
| ^ 164 | options={[
165 | { state: 'AL' },
166 | { state: 'AK' },
字头:
function StateInput() {
const [selected, setSelected] = useState([]);
return (
<>
<Typeahead
id="basic-typeahead-example"
labelKey="state"
onChange={(e) => this.onChangeState(e)}
options={[
{ state: 'AL' },
{ state: 'AK' },
{ state: 'AZ' },
{ state: 'AR' },
{ state: 'CA' }
]}
placeholder="Choose a state..."
selected={selected}
value={setSelected}
/>
</>
)
}
onChange函数:
onChangeState(e) {
this.setState({
state: e.target.value
})
}
【问题讨论】:
标签: javascript reactjs forms react-bootstrap react-bootstrap-typeahead