为什么一个基本的react-native redux app throw" a找不到商店错误"

时间:2016-11-21 21:48:27

标签: react-native redux react-redux

我试图开始使用本机,我在基本应用上遇到以下错误

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

这是我的代码

import React, { Component, PropTypes } from 'react';
import { Text } from 'react-native';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { Provider, connect } from 'react-redux';
import thunk from 'redux-thunk';

import reducer from './reducers/counter';

const store = createStore(
  combineReducers({
    counter: reducer,
  }),
  {}, // initial state
  compose(
      applyMiddleware(thunk),
  ),
);

class App extends Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() {
  }

  render() {
    return (
      <Provider store={store}>
        <Text>Hello World</Text>
      </Provider>
    );
  }
}

App.propTypes = {
  dispatch: PropTypes.func.isRequired,
  counter: PropTypes.number,
};

function mapStateToProps(state) {
  return {
    counter: state,
  };
}

export default connect(mapStateToProps)(App);

这里是减速器:

// ./reducers/counter.js
export default (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}

1 个答案:

答案 0 :(得分:1)

尝试将<Provider>移出App组件。

应该是

<Provider store={store}>
   <App />
</Provider>

connect依赖于store组件的context内的<Provider>