React Redux 仅返回存储对象

问题描述 投票:0回答:2

redux的getState(),subscribe()等方法不会进入我的组件,而只有初始存储对象才出现在组件的props中。我可以用它来解决这个问题吗?

道具日志

商店:{待处理:假,fetchFailed:否,授权:假,哈希:“,用户名:'',}

App.js

import React from 'react';

import Main from './views/Main';
import Auth from './views/Auth';

import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';

import {Provider} from 'react-redux';
import initStore from './store/store';

const AppNavigator = createStackNavigator({
    Main: {
        screen: Main,
        path: 'Main',
    },
    Auth: {
        screen: Auth,
        path: 'Auth',
    },
}, {
    initialRouteName: 'Auth',
    defaultNavigationOptions: {
        header: null,
    },
});

const AppNavigationContainer = createAppContainer(AppNavigator);

const config = {
    pending: false,
    fetchFailed: false,
    authorized: false,
    hash: '',
    username: '',
};

const store = initStore(config);

const App = () => {
    return (
        <Provider store={store}>
            <AppNavigationContainer data={store} /> // Don't work data prop
        </Provider>
    );
};

export default App;

Store.js

import {createStore, applyMiddleware} from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import reducer from '../reducers/reducers';

const initStore = (preloadStore) => {
    const store = createStore(reducer, preloadStore, applyMiddleware(thunk, logger));
    return store;
};

export default initStore;

Auth.js

// some code...
export default connect(
    (store) => ({
        store,
    }),
    (dispatch) => ({
        dispatch,
    }),
)(Auth);
reactjs react-native redux react-redux react-navigation
2个回答
0
投票

使用无法将类似的道具传递到导航容器。您需要使用screenProps

但是,在这种情况下,看来您做错了什么。您不应该在上下文中访问store。而是公开要获取的特定状态项:https://react-redux.js.org/api/connect


0
投票

您误解了mapState的工作方式。参数不是商店实例,参数是商店状态

您自己的组件无论如何都不应该访问实际的商店实例。

请参见the React-Redux docs page on using mapStateToProps以获取说明。

© www.soinside.com 2019 - 2024. All rights reserved.