内部侦听器函数内部使用effect始终保持相同
我遇到了有关React应用程序的问题。我有一个处理mapbox
map实例的CUSTOMMAP组件。至少从我的角度来看,代码的关键部分如下:
const CustomMap = (props) => {
const location = useSelector((state) => state.routing.location);
...
useEffect(() => {
... map init ...
map.on('moveend', () => {
console.log(location);
});
}, [];
useEffect(() => {
console.log(location);
}, [list of dependencies]);
}
想法是在mapbox
识别操作 moveend 变量(与mapbox无关),从redux开始,然后对其做点事。由于仅需要一个处理程序,因此我将其注册在第一个使用
仅一次的中,但我期望每次move> move> moveend < /代码>是触发的。
这没有发生,我想知道为什么? 位置
始终与页面加载上的相同。关于React/Redux的哪个概念我误会了?在第二使用中记录的位置
始终是正确的(当前在redux中的一个),但是使用效果
触发依赖关系更改。
谢谢你!
I'm stuck with an issue regarding React app. I have a CustomMap component that handles the mapbox
Map instance. The key parts of the code, at least from my perspective are as follows:
const CustomMap = (props) => {
const location = useSelector((state) => state.routing.location);
...
useEffect(() => {
... map init ...
map.on('moveend', () => {
console.log(location);
});
}, [];
useEffect(() => {
console.log(location);
}, [list of dependencies]);
}
The idea was to register a listener on mapbox
recognized action moveend
, get the custom location
variable (unrelated to mapbox) at that moment from Redux and do something with it. Since only one handler is required, I registered it in the first useEffect
that is called only once, but I expected the value location
will be fetched every time moveend
is triggered.
This is not happening and I wonder why? The location
is always the same as it is on page load. Which concept about React/Redux did I get wrong? The location logged in the second useEffect
will always be the correct one (one currently in Redux) but that useEffect
triggers on dependency change.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论