redux的getState(),subscribe()等方法不会进入我的组件,而只有初始存储对象才出现在组件的props中。我可以用它来解决这个问题吗?
商店:{待处理:假,fetchFailed:否,授权:假,哈希:“,用户名:'',}
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;
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;
// some code...
export default connect(
(store) => ({
store,
}),
(dispatch) => ({
dispatch,
}),
)(Auth);
使用无法将类似的道具传递到导航容器。您需要使用screenProps
。
但是,在这种情况下,看来您做错了什么。您不应该在上下文中访问store
。而是公开要获取的特定状态项:https://react-redux.js.org/api/connect
您误解了mapState
的工作方式。参数不是商店实例,参数是商店状态。
您自己的组件无论如何都不应该访问实际的商店实例。