简体   繁体   English

React - 在侦听器中访问全局范围时不会更新它

[英]React - The global scope is not updated when accessing it in a listener

I have a db listener that has to access the component properties (which can change over the full component's life).我有一个 db 侦听器,它必须访问组件属性(可以在整个组件的生命周期内更改)。 The problem I have is that when I am in the local scope of the listener, the global scope stuff is not updated.我遇到的问题是,当我在侦听器的本地范围内时,全局范围的内容不会更新。 For example, imagine that I have a prop "userData" which in a first instance has this form:例如,假设我有一个道具“userData”,它首先具有以下形式:

{
   name: "anthony"
}

Add changes its field value to "mark" after an unespecified time.添加在未指定的时间后将其字段值更改为“标记”。

If I run this code:如果我运行此代码:

console.log("Global: " + props.userData) <----------------- Before: {name: "anthony"} ; After: {name: "mark"} OK

const onPostsCollectionUpdate = (snapshot) => {
    /*
      This function gets the initial amount of posts 
      and the new ones that are uploaded in real time.
    */
    console.log("Local: " + props.userData) <----------------- Before: {name: "anthony"} ; After: {name: "anthony"} NOT UPDATED!

    ...
}

 useEffect(() => {
    const { firebase, attachListener } = props;

    // Create a Real-time Database listener
    const postsListener = firebase
      .getDatabase()
      .collection("posts")
      .doc(firebase.getCurrentUser().uid)
      .collection("userPosts")
      .orderBy("date")
      .limitToLast(MAX_POSTS_TO_RETRIEVE_LENGTH)
      .onSnapshot(onPostsCollectionUpdate); // Get the initial amount of posts

    // Attach the listener
    attachListener(postsListener);
  }, []);

I get this result:我得到这个结果:

Global: { "name": "anthony" }全局:{“名称”:“安东尼”}

Local: { "name": "anthony" }本地:{“名称”:“安东尼”}

Global: { "name": "mark" }全局:{“名称”:“标记”}

Local: { "name": "anthony" }本地:{“名称”:“安东尼”}

As you can see, inside the method "onPostsCollectionUpdate" the prop is not updated.如您所见,在方法“onPostsCollectionUpdate”中,道具没有更新。 I have also tried with a state variable and same result.我也尝试过使用状态变量和相同的结果。

Any ideas why this happen and how to get the most recent values in the local scope of the listener?任何想法为什么会发生这种情况以及如何在侦听器的本地范围内获取最新值?

Thank you.谢谢你。

Try to use this code尝试使用此代码

import React, { useCallback } from 'react';

console.log("Global: " + props.userData) <----------------- Before: {name: "anthony"} ; After: {name: "mark"} OK

const onPostsCollectionUpdate = (snapshot) => {
    /*
      This function gets the initial amount of posts 
      and the new ones that are uploaded in real time.
    */
    console.log("Local: " + props.userData) <----------------- Before: {name: "anthony"} ; After: {name: "anthony"} NOT UPDATED!

    ...
}

const initializeListener = useCallback(() => {
  const { firebase, attachListener } = props;

  // Create a Real-time Database listener
  const postsListener = firebase
    .getDatabase()
    .collection("posts")
    .doc(firebase.getCurrentUser().uid)
    .collection("userPosts")
    .orderBy("date")
    .limitToLast(MAX_POSTS_TO_RETRIEVE_LENGTH)
    .onSnapshot(onPostsCollectionUpdate); // Get the initial amount of posts

  // Attach the listener
  attachListener(postsListener);
}, [props]);

useEffect(() => {
  initializeListener();
}, [initializeListener]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM