我在Firebase中有一个配置文件文档集合,我想在配置文件页面中呈现它们,但是,在我更新了用户配置文件状态并使用useDispatch将状态存储在切片中之后,在呈现配置文件页面时,我得到了一个无限循环.

我已经try 将Dispatch()放入useEffect中,而不是放在useEffect和querySnapshotpromise 中,但无论我把它放在哪里,都会得到无限循环.

我们非常感谢您的反馈,谢谢.

\\ profiles.js

export const Profiles = () => {
  const [userProfiles, setUserProfiles] = useState([]);
  const dispatch = useDispatch();

  const navigate = useNavigate();
  const user = useSelector(selectUser);

  db.collection("customers")
    .doc(user.info.uid)
    .collection("profiles")
    .get()
    .then((querySnapshot) => {
      const documents = querySnapshot.docs.map((doc) => doc.data());
      setUserProfiles(documents);
    });

  useEffect(() => {
    dispatch(profiles(userProfiles));
  }, []);

  console.log({ userProfiles });

  return (
    <div className="profile_container">
      <h1 className="profile_title">Who's Watching?</h1>
      <div className="profile_row">
        {userProfiles.map((profile) => {
          return (
            <div className="profile_individualProfile">
              <img
                src="https://occ-0-300-1167.1.nflxso.net/dnm/api/v6/K6hjPJd6cR6FpVELC5Pd6ovHRSk/AAAABY5cwIbM7shRfcXmfQg98cqMqiZZ8sReZnj4y_keCAHeXmG_SoqLD8SXYistPtesdqIjcsGE-tHO8RR92n7NyxZpqcFS80YfbRFz.png?r=229"
                alt="profile"
              />
              <p>{profile.name}</p>
            </div>
          );
        })}
        <div
          onClick={() => navigate("/add-profile")}
          className="profile_addProfile_container"
        >
          <img
            src="https://img.icons8.com/ios-glyphs/30/FFFFFF/plus--v1.png"
            alt="add profile"
          />
          <h2>Add Profile</h2>
        </div>
      </div>
    </div>
  );
};
\\ userSlice.js

export const userSlice = createSlice({
  name: "user",
  initialState: {
    user: {
      info: null,
      profiles: [],
    },
  },
  reducers: {
    login: (state, action) => {
      state.user.info = action.payload;
    },
    logout: (state) => {
      state.user.info = null;
    },
    profiles: (state, action) => {
      state.user.profiles.push(action.payload);
      },
  },
});

推荐答案

在当前的实现中,当您的页面呈现时,db.collections运行,您设置状态setUserProfiles(documents),呈现您的应用程序,并再次运行db.collections.为了避免这种情况,您应该在useEffect中运行db.collections.

// fetch users only when your app renders
useEffect(() => {
    db.collection("customers")
      .doc(user.info.uid)
      .collection("profiles")
      .get()
      .then((querySnapshot) => {
        const documents = querySnapshot.docs.map((doc) => doc.data());
        setUserProfiles(documents);
      });
  }, []);

有另一种使用效果

useEffect(() => {
    dispatch(profiles(userProfiles));
  }, [userProfiles]);

这也行不通.setUserProfiles个将会引起问题.因为当应用程序渲染时,您获取数据,设置状态,更改userProfiles,这将再次重新渲染应用程序.

您的代码的问题是您不需要setUserProfiles.相反,在db.collections()中,当您收到文档时,您可以发送文档,然后使用useSelector从redux访问配置文件

// fetch users only when your app renders
useEffect(() => {
    db.collection("customers")
      .doc(user.info.uid)
      .collection("profiles")
      .get()
      .then((querySnapshot) => {
        const documents = querySnapshot.docs.map((doc) => doc.data());
        // setUserProfiles(documents); You do not need this
        dispatch(profiles(userProfiles))
      });
  }, []);

现在用useSelector到达redux的状态

// assuming reducers name is "users"
  const usersState = useSelector((state) => state.users);

现在,当您使用 map 时,请保护您的应用程序

 // make sure you use the correct data
 // you migh need to destructure
 {usersState && usersState.map((profile) => {

Reactjs相关问答推荐

react-hook-form问题:为什么getValues不返回大多数当前值?

LocalStore未存储正确的数据

为什么VScode中的React应用程序无法识别文件夹 struct ?

为什么安装FLOBIT后,所有的输入FIELD现在都有一个蓝色的轮廓?

如何使Reaction Select 选项菜单从选项数组的特定索引开始

react 路由GUGUD Use Effect无法通过useNavigate正常工作

使用`Link`包装`tr`标记会在Next.js中产生水合错误14

可以使用mode.css/mode.scss引用常规的类名吗?

使用Ionic 7和React 18,如何访问历史对象以在页面之间导航?

根据用户 Select 的注册类型更改表单字段

React-router动态路径

如何读取 null 的属性?

React - 拖放 UML

null 不是对象(判断RNSound.IsAndroid)

antd 找不到 comments

我应该使用 useEffect 来为 React Native Reanimated 的 prop 值变化设置动画吗?

不能在我的代码中使用 .给我一个警告!有人知道解决方案吗?

错误:无效挂钩调用.钩子只能在函数组件的主体内部调用.使用 next/router 时

多次响应获取发送请求

如何在 NavBar 中设置动态标题