更新对React上下文状态的更新
我正在通过儿童组件中的回调更新上下文中的共享状态,但这不会导致重新渲染,这导致儿童组件的上下文具有初始值,直到下一个重新渲染。
是否有一种方法可以迫使儿童更新和一旦状态在上下文提供商中更新一旦状态进行更新?
我的上下文提供商:
const UserLocationContext = React.createContext()
export const useUserLocation = () => {
return useContext(UserLocationContext)
}
export const UserLocationProvider = ({ children }) => {
const [ipUserLocation, setIpUserLocation] = useState(null)
const updateIpUserLocation = (ipUserLocation) => {
setIpUserLocation(ipUserLocation)
console.log(ipUserLocation) //value is updated here immediately after the updateIpUserLocation call
}
return (
<UserLocationContext.Provider value = {{ipUserLocation, updateIpUserLocation}}>
{children}
</UserLocationContext.Provider>
)
}
export default UserLocationProvider
孩子:
const LocationHandler = () => {
const {ipUserLocation, updateIpUserLocation} = useUserLocation()
useEffect(() => {
const ip_url = `https://api.freegeoip.app/json/`
const fetchIPLocation = async () => {
const result = await fetch(ip_url);
const json = await result.json();
updateIpUserLocation([json.latitude, json.longitude])
console.log(ipUserLocation) //value here remains null until next re-render
}
fetchIPLocation()
}, []);}
I'm updating the shared state in context through a callback in the child component, but that does not cause a re-render, which results in the context in the child component having an initial value until the next re-render.
Is there a way to force the update of the child and a re-render once state is updated in the context provider?
My context provider:
const UserLocationContext = React.createContext()
export const useUserLocation = () => {
return useContext(UserLocationContext)
}
export const UserLocationProvider = ({ children }) => {
const [ipUserLocation, setIpUserLocation] = useState(null)
const updateIpUserLocation = (ipUserLocation) => {
setIpUserLocation(ipUserLocation)
console.log(ipUserLocation) //value is updated here immediately after the updateIpUserLocation call
}
return (
<UserLocationContext.Provider value = {{ipUserLocation, updateIpUserLocation}}>
{children}
</UserLocationContext.Provider>
)
}
export default UserLocationProvider
Child:
const LocationHandler = () => {
const {ipUserLocation, updateIpUserLocation} = useUserLocation()
useEffect(() => {
const ip_url = `https://api.freegeoip.app/json/`
const fetchIPLocation = async () => {
const result = await fetch(ip_url);
const json = await result.json();
updateIpUserLocation([json.latitude, json.longitude])
console.log(ipUserLocation) //value here remains null until next re-render
}
fetchIPLocation()
}, []);}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
usestate
是异步的,因此ipuserlocation
值在setipuserlocation
被调用之后不会立即更新。对于修复程序,您可以添加
ipuserlocation
作为useffect
的依赖性,该将帮助您收听ipuserlocation
onlocation> location> location> location> location>
。The problem is
useState
is asynchronous, soipUserLocation
value is not updated immediately aftersetIpUserLocation
gets called.For the fix, you can add
ipUserLocation
as a dependency touseEffect
that would help you to listen to all changes fromipUserLocation
onLocationHandler
.实际上,当您更新上下文状态时,子组件会重新渲染。之所以发生这种情况,是因为您使用 usecontext 挂钩来聆听对上下文所做的任何更改。您可以采取的措施实际向自己证明孩子被重新渲染的是在孩子的组件中添加此内容:
使用此使用效果 console.log 每次重新渲染孩子时都会运行,并且< strong> ipuserlocation 已经改变。
文档:
Actually the child component gets re-rendered when you update the state of the context. And this happens because you are using the useContext hook to listen to any change made to the context. What you can do to actually prove to yourself that the child gets re-rendered is adding this in the child component:
With this useEffect the console.log will run everytime the child gets re-rendered and the ipUserLocation has changed.
Docs: https://reactjs.org/docs/hooks-effect.html