简体   繁体   English

当 Firebase 变量更改时更新 React Native Screen

[英]Update React Native Screen when Firebase variable changes

Hi I'm new to firebase and don't know how to make a screen in my react native app update when a certain firebase variable changes.嗨,我是 firebase 的新手,当某个 firebase 变量发生变化时,我不知道如何在我的反应原生应用程序更新中制作屏幕。

In my app's homescreen, a user's posts are fetched from firebase in componentDidMount , and then rendered:在我的应用程序的主屏幕中,从componentDidMount中的 firebase 获取用户的帖子,然后呈现:

componentDidMount = () => {
  this.setup();
};

setup = async () => {
  const { currentUser } = await firebase.auth();
  this.setState({ currentUser });

  await firebase
    .database()
    .ref("users/" + currentUser.uid + "/posts")
    .on("value", snapshot => {
      this.setState({posts: snapshot.val()})
    });
}

// render posts

In a separate screen, the user can choose to add a post and the firebase database is updated:在单独的屏幕中,用户可以选择添加帖子并更新 firebase 数据库:

addPost = async () => {
  const { currentUser } = firebase.auth();

  await firebase
    .database()
    .ref("users/" + currentUser.uid + "/posts")
    .push({
      post: // data for post
    });

However, though the database is successfully changed, the homescreen doesn't update and show the newly added post till it is manually reloaded.但是,尽管数据库已成功更改,但在手动重新加载之前,主屏幕不会更新并显示新添加的帖子。 How do I add a listener to the homescreen, so that when the posts database variable changes, the screen automatically updates.如何在主屏幕上添加一个监听器,以便当帖子数据库变量更改时,屏幕会自动更新。

If you want to share the same data between multiple screens, the best is to use Redux.如果要在多屏之间共享相同的数据,最好使用Redux。

Using Redux, all your renders function (which needs a specific data from the store) will automatically refresh if a data is updated.使用 Redux,如果数据更新,您的所有渲染 function(需要来自商店的特定数据)将自动刷新。 That way, you can only handle one firebase listener to update your redux store.这样,您只能处理一个 firebase 侦听器来更新您的 redux 存储。

I would create a "Post" reducer and dispatch an update action every time I got something new from the firebase listener.每当我从 firebase 监听器获得新内容时,我都会创建一个“发布”减速器并调度一个更新操作。

// Actions
const ADD = 'post/ADD';
const UPDATE = 'post/UPDATE';

// Initial state
const initialState = {
  posts: [],
};

// Reducer
export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case ADD:
      return {
        ...state,
        posts: [...state.posts, action.post]
      };
    case UPDATE:
      return {
        ...state,
        posts: action.posts
      };
    default:
      return state;
  }
}

// Action Creators
export function addPost(post) {
  return {
    type: ADD,
    post
  };
}

export function updatePosts(posts) {
  return {
    type: UPDATE,
    posts
  };
}

And the listener would be like:听众会是这样的:

import { update } from 'PostRedux.js'

firebase
    .database()
    .ref("users/" + currentUser.uid + "/posts")
    .on("value", snapshot => {
      Store.dispatch(update(snapshot.val()));
    });

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

相关问题 React Native Hot Reload 不会在更改时更新屏幕? - React Native Hot Reload doesn't update the screen on changes? React-native,firestore:当实例变量改变其值时如何更新组件状态 - React-native, firestore: how to update component state when an instance variable changes its value 聆听变化,firebase反应本机 - listen for changes firebase react native 在 React Native 中使用 Firebase(Firestore) 列出数据时导致空白屏幕 - Resulting in a blank screen when listing data with Firebase(Firestore) in React Native 反应本机 firebase 离线写入+在线更新不同步 - react native firebase offline write + update not sync when online 屏幕更新时看不见的变化 - Invisible changes when screen update 当 window 变量在反应中发生变化时,如何更新 state - How do I update state when a window variable changes in react 使用反应挂钩更改导入的变量时更新 state - Update state when imported variable changes using react hooks React-Native 组件在我保存更改时更新,而不是在启动时更新。 [Firebase,使用效果] - React-Native component updates when I save changes, and not upon starting. [Firebase, useEffect] 当我们从 firebase 获取/更新数据时,如何在 react-native 中更新组件而不刷新/重新加载? - How to update a component without refresh/reload in react-native when we fetch/update data from firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM