Здравствуйте, я пытаюсь использовать избыточный код в приложении для реагирования и получаю ошибку, упомянутую в названии, в частности Invariant Violation: 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)"
. Я не уверен, в чем проблема, и хотя я следовал за подобными темами, пока ничего не работало.
Ниже - мой источник App.js
import React, { Component } from 'react';
import { Provider, connect } from 'react-redux';
import { Platform, StyleSheet, Text, View } from 'react-native';
import * as utils from './src/utils';
import store from './src/store';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
class App extends React.Component {
constructor(...args) {
super(...args);
}
login({ username, password }) {
let requestData = new FormData();
requestData.append('username', username);
requestData.append('password', password);
console.log('[Login]: Attempt');
console.log(JSON.stringify(requestData, null, 4));
fetch('https://my-api.lab/user/login', {
method: 'POST',
body: requestData
}).then(res => res.json()).then((json) => {
console.log('[Login]: Response');
console.log(JSON.stringify(json, null, 4));
if (json.authenticated) {
console.log('[Login]: Success');
this.props.userLoginSuccess(json);
} else {
console.log('[Login]: Failure');
}
}).catch((err) => {
console.log('[Login]: error');
console.log(JSON.stringify(err, null, 4));
});
}
componentDidMount() {
console.log('App mounted!');
this.login({
username: 'asdf',
password: 'asdf'
});
}
render() {
let username = Object.keys(this.props.user).length ? this.props.user.data.post.username : '';
return (
<Provider store={store}>
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome {username} to React Native!
</Text>
</View>
</Provider>
);
}
}
const mapStateToProps = (state) => {
return {
user: state.userReducer
}
}
const mapDispatchToProps = (dispatch) => {
return {
userLoginSuccess: (user) => {
dispatch({
type: 'USER_LOGIN_SUCCESS',
payload: user
})
},
userLoginFailure: (user) => {
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
Также вот мой файл index.js
import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('pushNotificationsNative', () => App);
В магазине
import { createStore } from 'redux';
import rootReducer from '../reducers/index.js';
const store = createStore(
rootReducer
);
export default store;
и, наконец, простой редуктор
import deepExtend from 'deep-extend';
const initialState = {};
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'USER_LOGIN_SUCCESS': {
return deepExtend({}, state, action.payload);
}
default: {
return deepExtend({}, state);
}
}
}
export default userReducer;