Je crée un nouveau projet natif réactif et j'écris un démo redux.Le simulateur IOS affiche l'erreur `` Attendait que le réducteur soit une fonction ''. J'ai essayé de résoudre le problème à partir des réponses de l'aperçu, mais cela ne fonctionne pas
index.ios.js
import React, {Component} from 'react';
import {AppRegistry, StyleSheet, Text, View} from 'react-native';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import {reducers} from './src/reducer';
import {App} from './src/App';
const store = createStore(reducers)
export default class rnredux extends Component {
render() {
return (
<View style={styles.container}>
<Provider store ={store}>
<App/>
</Provider>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
});
AppRegistry.registerComponent('rnredux', () => rnredux);
App.js
import {connect} from 'react-redux';
import MyComponent from './myComponent';
function mapStateToProps(state) {
return {text: state.text, name: state.name}
}
function mapDispatchToProps(dispatch) {
return {
onChange: (e) => dispatch({type: 'change', payload: e.target.value})
}
}
const App = connect(
mapStateToProps,
mapDispatchToProps
)(MyComponent);
export default App;
myComponent.js
import React,{Component} from 'react';
import {Text,TextInput} from 'react-native';
export default class myComponent extends Component{
render(){
<View>
<Text>{this.props.text}</Text>
<TextInput defaultValue = {this.props.name} onChangeText = {this.props.onChange}></TextInput>
</View>
}
}
reducer.js
import {combineReducers} from 'redux';
const reducerAction = (state = {
text: '你好,访问者',
name: '访问者'
}, action) => {
switch (action.type) {
case 'change':
return {
name: action.payload,
text: '你好,' + action.payload
};
default:
return state;
}
}
const reducers = combineReducers({
reducerAction
})
export default reducers;
Changement
import {reducers} from './src/reducer';
import {App} from './src/App';
à
import reducers from './src/reducer';
import App from './src/App';
Lorsque vous importez la valeur par défaut du module, n'utilisez pas brace{}
.