【问题标题】:React Redux <Provider> returning only store objectReact Redux <Provider> 仅返回存储对象
【发布时间】:2020-02-16 21:37:15
【问题描述】:

redux 的 getState()、subscribe() 等方法没有进入我的组件,而只有初始的 store 对象进入组件的 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


    【解决方案1】:

    使用不能将这样的道具传递给导航容器。您需要使用screenProps。

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

    【讨论】:

    • 工作!谢谢你:)
    【解决方案2】:

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

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

    有关说明,请参阅the React-Redux docs page on using mapStateToProps。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      相关资源
      最近更新 更多