【发布时间】:2020-04-11 10:04:59
【问题描述】:
我对 React 还很陌生。
我有选择器,它返回用户选择的任何内容。
代码示例:
handleChanged(e){
const { onSelectcountry } = this.props;
onSelectcountry(e.target.value)
}
return (
<div>
<Input type="select" name="select" value={Country} onChange={this.handleChanged.bind(this)}>
{
country.map((item) => {
return (<option value={item._id} key={item._id}> {item.name}</option>);
})
}
</Input>
</div>
);
我调度动作取决于用户选择,
import { fetchNews} from '../../actions';
getNews(filterNews) {
const { fetchNews } = this.props;
fetchNews(filterNews);
}
onSelectcountry(country) {
this.setState({ Country: country});
this.getNews({
this.state,
})
}
<CountrySelector onSelectcountry={this.onSelectcountry.bind(this)} Country={Country}/>
问题是:当所选值更改时,它显示了先前选择的值。
【问题讨论】:
-
这可能是一个 setState 同步问题 - 你能通过 CodeSandbox 发布代码吗?。