我试图开始使用本机,我在基本应用上遇到以下错误
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
}
}
答案 0 :(得分:1)
尝试将<Provider>
移出App
组件。
应该是
<Provider store={store}>
<App />
</Provider>
connect
依赖于store
组件的context
内的<Provider>
。