简体   繁体   English

在不同的屏幕中设置 Redux 存储监听器

[英]set up Redux store listener in different screen

Im pretty new to redux therefore please excuse me if my explanation is a little bit sloppy: I have a complex react native app using the redux store but also firebase stuff and react navigation.我对 redux 很陌生,因此,如果我的解释有点草率,请原谅:我有一个使用 redux 商店的复杂反应原生应用程序,还有 firebase 东西和反应导航。 Im declaring my redux store in a parents js file which wraps all other screens with a navigator, declaration of store looks like this:我在父 js 文件中声明我的 redux 存储,该文件用导航器包装所有其他屏幕,存储声明如下所示:

const rootReducer = combineReducers({
  auth: authReducer,
  myPigeons: pigeonReducer,
  myBadges: badgesReducer
});

const store = createStore(rootReducer, applyMiddleware(ReduxThunk));

I know that I could in theory now apply something like that to my store directly underneath:我知道理论上我现在可以将类似的东西直接应用到我的商店下面:

store.subsribe(state => console.log(state));

and every time the store would change his state, it would be shown in my console.每次商店更改他的 state 时,它都会显示在我的控制台中。 The problem I face through is that I want to subscibe to the store, just not in the same js file.我面临的问题是我想订阅商店,而不是在同一个 js 文件中。

What I want to accomplish is to listen to store changes in the entire app, somewhere in a navigator child component and automaticly detect when the store changes.我想要完成的是监听整个应用程序中的存储更改,在导航器子组件中的某个位置,并自动检测存储何时更改。

Going in detail, I am using firebase listeners within one of my screens.详细来说,我在我的一个屏幕中使用 firebase 监听器。 As soon as I want to change the user in my app (thats where I reset my whole redux store, thats why I want a redux store listener) the firebase listeners dont stop because I need to stop them in the same js file I initialized them, and therefore I will need to implement the redux store listener inside the exact same file, which is NOT the same file in which I created my redux store.一旦我想在我的应用程序中更改用户(这就是我重置整个 redux 商店的地方,这就是为什么我想要一个 redux 商店监听器)firebase 商店监听器)firebase 文件初始化需要停止它们,因为我需要在相同的监听器文件中停止它们,因此我需要在完全相同的文件中实现 redux 存储侦听器,该文件与我创建 redux 存储的文件不同。 I need to listen to redux changes inside my chatscreen and as soon as I change user I need to register that redux store is empty and call my firebase listeners to stop.我需要在我的聊天屏幕中收听 redux 的变化,一旦我更改用户,我需要注册 redux 商店是空的,并打电话给我的 firebase 听众停止。

Do you think this is possible and if yes, how I could do that?你认为这是可能的,如果是的话,我该怎么做?

If the problem is caused by this, an approach can be made.如果问题是由此引起的,可以采取一种方法。

On the page created in the Store:在商店中创建的页面上:

export const store = createStore(rootReducer, applyMiddleware(ReduxThunk));

On the page to be used in the Store:在要在商店中使用的页面上:

import {store} from '....'

store.subscribe (() => {
    // You can choose your reducers
    let {auth, myPigeons} = store.getState();
    console.log(auth);

    // Brings up the whole store
    let state = store.getState();
    console.log(state);
});

So you can subscribe to your store on any page.因此,您可以在任何页面上订阅您的商店。 However, try to avoid the memory leak when using subscriber events.但是,在使用订阅者事件时,请尽量避免 memory 泄漏。 When you're done, cancel your subscription.完成后,取消订阅。

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

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