2017-03-16 72 views
3

我创建了一个新的react-native项目并编写了一个redux demo.IOS Simulator显示错误'Expected the reducer to a function'。我尝试从预览答案中解决问题,但它不工作预计reducer是一个函数

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

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; 

回答

9

变化

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

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

当你导入模块的默认,dont't使用支具{}

+0

您能解释一下 {redurs}和redurs之间的区别。它虽然为我工作,但我仍然想知道。 –

+0

@Vijender Kumar'redurs'用于导入使用模块导出的关键字'default'的函数或变量,'{reducers}'用于导入其他函数。默认的函数就像nodejs中的'module.exports',其他的函数没有'default'就像'exports'。 – wuxiandiejia

相关问题