无法在Connect(App)的上下文或道具中找到“store”

时间:2017-10-09 04:43:04

标签: react-native redux react-redux

我不知道为什么会收到错误。我确实在我的代码中包含了所有必需的库。顺便说一句,我使用navReducer

这是我的getStore代码:

export default function getStore(navReducer) {
const store= createStore(
    getRootReducer(navReducer),
    undefined,
    applyMiddleware(thunk)
);
return store;
}

问题在于我的index.js,在那里我命名了类App

const AppNavigator = StackNavigator
(
Routes, 
{
    navigationOptions: {
        title: ({ state }) => {
            if (state.params) {
                return `${state.params.title}`;
            }
        }
    },
    initialRouteName:'Login',
    headerMode:'none'
    }
    );

    const navReducer = (state, action) => {
    const newState = AppNavigator.router.getStateForAction(action, state);
    return newState || state;
    };

     const store = getStore(navReducer);

   class AppWithNavigationState  extends Component {
   render() {
    return (
        <AppNavigator
            navigation={addNavigationHelpers({
                dispatch: this.props.dispatch,
                state: this.props.nav,
            })}
        />
        );
        }
        }

AppWithNavigationState.propTypes = {
dispatch: PropTypes.func.isRequired,
nav: PropTypes.object.isRequired,
};

const mapStateToProps=(state)=>{
return {
  nav:state.nav  
}};

export class App extends Component{
render() {
    return (
        <Provider store={store}>
            <AppWithNavigationState />
        </Provider>
    );
    }
    }
    export default connect(mapStateToProps)(App);

抱歉,我的代码太长了,但我认为包含它很重要。

1 个答案:

答案 0 :(得分:1)

您不应在connect上使用Provider,请尝试:

export default App;

而不是

export default connect(mapStateToProps)(App);