无法在“Connect(App)”的上下文或道具中找到“store”。我有一个 已经包装组件

问题描述 投票:2回答:1

不变违规:无法在“连接(应用)”的上下文或道具中找到“存储”。将根组件包装在a中,或者将“store”显式传递为“Connect(App)”的prop。

我不愿问多次问过的问题的变种,但我已经尝试了所有提出的解决方案而没有运气。 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

reactjs redux
1个回答
0
投票

您尚未向App组件提供商店。这就是为什么它无法连接组件与reducer:

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

从app.js中删除提供程序

© www.soinside.com 2019 - 2024. All rights reserved.