在React/Redux中调度操作的问题

Bay*_*ife 4 javascript reactjs redux

我是React和Redux的新手,在我第一次使用它时遇到了一些问题.我尝试按照Redux Doc中的示例进行操作,但我很难理解所有内容,因为每个示例都在ES5 - 6甚至7语法之间跳转.

但是,当我尝试调度操作时,我收到以下错误

Uncaught TypeError: (0 , _index2.default) is not a function
Run Code Online (Sandbox Code Playgroud)

错误信息

我知道SO社区在一个问题中不喜欢这么多代码,但我不知道问题来自哪里.对不起!

这些是我的代码:

Index.js

import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import configureStore from './store/configureStore'

const store = configureStore()

render(
  <Provider store={store}>
    <App />
   </Provider>,
   document.getElementById('root')
 )
Run Code Online (Sandbox Code Playgroud)

我的商店

import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import index from '../reducers'

export default function configureStore(preloadedState) {
  const store = createStore(
    index,
    preloadedState,
    applyMiddleware(thunkMiddleware, createLogger())
   )

   if (module.hot) {
      // Enable Webpack hot module replacement for reducers
      module.hot.accept('../reducers', () => {
        const nextRootReducer = require('../reducers').default
        store.replaceReducer(nextRootReducer)
     })
    }

   return store
 }
Run Code Online (Sandbox Code Playgroud)

我的容器组件

import React, { Component, PropTypes } from 'react'
import AddTodo from '../components/AddTodo'
import { connect } from 'react-redux'
import addItem from '../actions/index'

class App extends Component {
   constructor(props) {
    super(props)
    this.handleClick = this.handleClick.bind(this)
}

handleClick(e){
    console.log("click")
    console.log(e);
    const {dispatch} = this.props
    dispatch(addItem(e));
}

render() {
    return (
        <div>
        < h1 > Hallo </h1>
            <AddTodo handleAddItem={this.handleClick}/>
        </div>
       )
    }
}

App.propTypes = {
   dispatch: PropTypes.func.isRequired
}

function mapStateToProps(state){
   return {
      AddTodo
   }
 }

 export default connect (mapStateToProps)(App)
Run Code Online (Sandbox Code Playgroud)

我的孩子成分:

import React, { Component, PropTypes } from 'react'
import addItem from '../actions/index'

export default class AddTodo extends Component {
   constructor(props) {
    super(props)
    this.handleClick = this.handleClick.bind(this)
    this.state = {newItem: ''}
  }

onChange(e){
    console.log("change")
    console.log(e.target.value);
    this.setState({newItem: e.target.value})
}

handleClick(e){
    this.props.handleAddItem(this.state.newItem)
   // const {dispatch} = this.props
   // console.log("clickc")
   // console.log(this.state.newItem);
   // dispatch(addItem(this.state.newItem))
}

render() {
    return (
        <div>
            <h3>Add Item </h3>
            <input
                type="text"
                value={this.state.newItem}
                onChange={this.onChange.bind(this)}
                />
            <button onClick={this.handleClick}>Hallo</button>
        </div>
    )
   }
  }
Run Code Online (Sandbox Code Playgroud)

减速机

 export default (state = [], action) => {
   switch (action.type){
    case 'ADD_ITEM':
        return action.item
    }
  }
Run Code Online (Sandbox Code Playgroud)

最后是行动

export function addItem(item){
   console.log("addTOdo")
   return {
      type: 'ADD_ITEM',
      item
    }
  }
Run Code Online (Sandbox Code Playgroud)

我希望有人可以在这里帮助我,坐几个小时,不知道发生了什么.

Yur*_*nko 7

您没有将动作创建者导出为默认值.你需要

export default function addItem(item){
   console.log("addTOdo")
   return {
      type: 'ADD_ITEM',
      item
    }
  }
Run Code Online (Sandbox Code Playgroud)

要么

import {addItem} from '../actions/index'
Run Code Online (Sandbox Code Playgroud)