期望减速器起作用 [英] Expected the reducer to be a function

查看:55
本文介绍了期望减速器起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的react-native项目并编写了一个redux演示.IOS Simulator显示错误``Expider the reducer is a function''.我试图从预览答案中解决问题,但不起作用

I create a new react-native project and write a redux demo.IOS Simulator show the error 'Expected the reducer to be a function'.I tried to solve the problem from the preview answers,but it doesn't work

index.ios.js

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

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

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;

推荐答案

更改

import {reducers} from './src/reducer';
import {App} from './src/App';

import reducers from './src/reducer';
import App from './src/App';

导入模块的默认值时,请勿使用大括号 {} .

When you import the module's default, dont't use brace{}.

这篇关于期望减速器起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆