Could not find "store" in either the context or props of "Connect(App)". I have a <Provider> wrapping the component already

Viewed 5206

Invariant Violation: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(App)".

I hate to ask a variation of a question that has been asked many times, but I have tried all the proposed solutions with no luck. https://codesandbox.io/s/0pyl7n315w

index.js

import React, {Component} from 'react'
import {AppRegistry} from 'react-native'
import {Provider} from 'react-redux'
import App from './app'

import configureStore from './store.js'
const store = configureStore();

class MyCounterApp extends Component {
    render() {
        return(
            <Provider store={store}>
            <App/>
            </Provider>
        )
    }
}

AppRegistry.registerComponent('MyCounterApp', () => MyCounterApp)

app.js

import React from 'react';
import {Button, Text, View} from 'react-native';
import {addToCounter} from "./actions";
import { connect } from 'react-redux';

class App extends React.Component {

    handleOnClick = event => {
        this.props.addToCounter()
    };

    render() {
        return (
                <View>

                    <Text>{this.props.count}</Text>

                    <Button onPress={() => this.props.addToCounter()}
                         title={"Click Me!"}>
                     </Button>

                </View>
    )
    }
}

function mapDispatchToProps(dispatch) {
    return {
        addToCounter: () => dispatch(addToCounter())
    }
}

function mapStateToProps(state) {
    return {
        count: state.count
    }
}


export default connect(mapStateToProps, mapDispatchToProps)(App)

store.js

import reducer from './reducer'
import {createStore} from 'redux'

export default function configureStore() {
    let store = createStore(
        reducer
    )
    return store
}

reducer.js

import {ADD_TO_COUNTER} from './actions'

const initialState = {
    counter: 0
}


const reducer = (state = initialState, action) => {

    switch (action.type) {
        case ADD_TO_COUNTER:
            return {
                ...state,
                counter: state.counter + 1
            }
        default:
            return state
    }
}

export default reducer;

I am following along with this tutorial: https://medium.com/@pavsidhu/using-redux-with-react-native-9d07381507fe

2 Answers

If you want to test components without using store, the first thing you have to do is export your disconnected component like: export { Component }.

Then you'll have to import on your test file like that: import { Component } from ...

If the error persists, look to see if the component uses components that are connected, and if yes, you have to mock that one, for example: jest.mock('../../../AnotherComponent', () => ChildComponent => props => <ChildComponent {...props} />);

Example:

const Component = (props: Props) => (
        <>
            ...
            <AnotherComponent // connected component inside <Component />
                {...props}
            />
        </>
    )

const mapStateToProps = (state: State) => ({});
const mapDispatchToProps = {

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Component);

export { Component }; // disconnected component

You have not provided store to your App component. that's why it is failed to connect component with reducer:

class MyCounterApp extends Component {
    render() {
        return(
            <Provider store={store}>
                <App/>
            </Provider>
        )
    }
}

Remove provider from app.js

Related