【发布时间】:2021-01-10 10:05:25
【问题描述】:
鉴于类似:
function MapControl() {
const [countries, setCountries] = useContext(CountriesContext)
useEffect( () => {
ThirdPartyApi.OnSelectCountry((country) => {
setCountries([...countries, country])
})
})
return (<ThirdPartyApi.Map />)
}
我遇到的问题是对setCountries 的调用无法按预期工作,因为countries 数组未从ThirdPartyApi 提供的自定义事件处理程序的上下文中更新。
什么是建模的简洁方法?我可以在事件处理程序中更新一个本地可变数组,但这不会从其他组件中获取对 countries 的任何更改,因此感觉注定会导致问题。
【问题讨论】:
标签: javascript reactjs react-hooks react-context