我正在尝试获取dropdown
更改事件的数据。首先我在加载时加载list of banks
或加载了组件。现在我想在用户选择任何state
时获取bank
列表。我收到此错误
Uncaught (in promise) TypeError: _this3.setState is not a function
at eval (rro780nklq.codesandbox.io/src/form_component.js:71)
at <anonymous>
我试过这个
render() {
return (
<div>
<DropDown
data={this.state.data}
defaultOption="select Bank"
callbackFn={this.callStateService}
/>
<DropDown data={this.state.stateData} defaultOption="select State" />
<DropDown data={this.state.district} defaultOption="select District" />
</div>
);
}
}
onchange事件
onChangeDropdown = e => {
console.log(e.target.value);
this.props.callbackFn(e.target.value);
};
答案 0 :(得分:0)
您可以将callStateService
定义为箭头功能,以保留this
:
callStateService = bankname => {
...
答案 1 :(得分:0)
您可以使用箭头功能删除与setState相关的错误,但除此之外还有更多错误。你需要检查
callStateService = (bankname) => {
axios
.get(
"your url"
bankname
)
.then(response => {
console.log(response.data.data);
const d = response.data.data;
this.setState({
stateData: d
});
});
}