与redux错误反应原生

时间:2017-01-03 19:09:33

标签: react-native redux

我按照演示创建了一个项目。我使用react-native和redux,然后我得到了这个错误" undefined不是一个函数(评估'(0,_redux.createStore)(_ todoListRedux.reducer)')`& #34;

这是我的代码:

index.android.js

import {AppRegistry, View} from 'react-native'
import {createStore} from 'redux'
import {reducer} from './todoListRedux'

const store = createStore(reducer)
import App from './App'

const AppWithStore = () => <App store={store} />

AppRegistry.registerComponent('redux', () => AppWithStore)

todoListRedux.js

export const types = {
  ADD: 'ADD',
  REMOVE: 'REMOVE',
}
export const actionCreators = {
  add: (item) => {
    return {type: types.ADD, payload: item}
   },
  remove: (index) => {
    return {type: types.REMOVE, payload: index}
  }
}
const initialState = {
  todos: ['Click to remove', 'Learn React Native','Write Code','Ship App'],
}
export const reducer = (state = initialState, action) => {
  const {todos} = state
  const {type, payload} = action
  switch(type) {
    case types.ADD: {
     return {
        ...state,
        todos: [payload, ...todos],
      }
    }
    case types.REMOVE: {
      return {
        ...state,
        todos: todos.filter((todo,i) => i !== payload),
      }
    }
  }
  return state
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

商店已添加到createStore。应该是combineReducers的商店。

据我所知,middleware应作为createStore中的第二个参数传递。

类似的东西。

import { applyMiddleware, createStore } from 'redux';
import rootReducer from '../store';

const middleware = applyMiddleware({you can pass here (logger or thunk, or the available options)});

//example
const middleware = applyMiddleware(thunk);

const store = createStore(rootReducer, middleware);