在“ Connect(App)”的上下文或道具中找不到“商店”。我已经有一个<provider>包装了组件

时间:2018-09-03 18:36:04

标签: reactjs redux

不变违反:在“ Connect(App)”的上下文或道具中找不到“商店”。将根组件包装在中,或者将“存储”作为道具明确传递给“ Connect(App)”。

我不想问一个已经问过很多次的问题,但是我尝试了所有提出的解决方案,但都没有运气。 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;

我将跟随本教程: https://medium.com/@pavsidhu/using-redux-with-react-native-9d07381507fe

2 个答案:

答案 0 :(得分:1)

如果要在不使用存储的情况下测试组件,则要做的第一件事是导出已断开连接的组件,例如:export { Component }

然后,您必须像这样import { Component } from ...

导入测试文件。

如果错误仍然存​​在,请查看该组件是否使用已连接的组件;如果是,则必须模拟该组件,例如: jest.mock('../../../AnotherComponent', () => ChildComponent => props => <ChildComponent {...props} />);

示例:

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

答案 1 :(得分:0)

您尚未为应用程序组件提供存储。这就是为什么将组件与reducer连接失败的原因:

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

从app.js删除提供程序