gpt4 book ai didi

react-native - Wix React-Native-Navigation v2 和 redux-persist

转载 作者:行者123 更新时间:2023-12-04 15:38:48 25 4
gpt4 key购买 nike

我正在使用 react-native-navigation 和 redux 进行状态管理。我将每个 Component 注册为 WrappedComponent,将其连接到 redux 存储。这非常有效,并且与官方 react-native-navigation 文档中的atoami 示例代码非常相似:https://wix.github.io/react-native-navigation/#/docs/showcases

import { Provider } from "react-redux";
import store from "./config/store";
...
function WrappedComponent(Component) {
return function inject(props) {
const EnhancedComponent = () => (
<Provider store={store}>
<Component {...props} />
</Provider>
);

return <EnhancedComponent />;
};
}

export function registerScreens() {
Navigation.registerComponent("Initializing", () =>
WrappedComponent(InitializingScreen)
);
Navigation.registerComponent("SignIn", () => WrappedComponent(SignInScreen));
...
}

存储对象为:
import { createStore, compose, applyMiddleware } from "redux";
import thunk from "redux-thunk";

import reducers from "../reducers";

const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

export default createStore(
reducers,
composeEnhancer(applyMiddleware(thunk))
);

但是,我找不到为那些包装的组件设置 redux persist 的方法。我不想在 WrappedComponent 内这样做函数,因为它会为每个单独的组件调用。我也找不到关于此的明确文档。

我想我也可以使用 AsyncStorage,但更喜欢将它与 Redux-persist 一起使用。有人知道怎么做吗?

最佳答案

这就是我处理 redux 的方式,redux 持续存在并且 wix v2 react-native-navigation

在您的 store.js

import {createStore,applyMiddleware} from "redux";
import rootReducer from './reducers/RootReducer';
import thunk from 'redux-thunk';
import {persistStore, persistReducer} from 'redux-persist';
import {compact} from "lodash";
import storage from 'redux-persist/lib/storage';


const persistConfig = {
key: 'app',
storage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer);

const middleware =compact([
thunk.withExtraArgument()
]);


export const store = createStore( persistedReducer,applyMiddleware(...middleware));
export const persistor = persistStore(store);

然后在您的 navigation.js或者你基本上注册屏幕的地方
import React from "react";
import {Navigation} from "react-native-navigation";
import {Provider} from 'react-redux';
import {PersistGate} from 'redux-persist/integration/react'
import {store, persistor} from "./config/store"; //Check this line

function WrappedComponent(Component) {
return function inject(props) {
const EnhancedComponent = () => (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Component {...props}/>
</PersistGate>
</Provider>
);
return <EnhancedComponent />;
};
}

// Then your normal registration
export function registerScreens() {
Navigation.registerComponent("Initializing", () =>
WrappedComponent(InitializingScreen)
);
Navigation.registerComponent("SignIn", () => WrappedComponent(SignInScreen));
...
}

关于react-native - Wix React-Native-Navigation v2 和 redux-persist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56487838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com