Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems to dispatch action in React/Redux

I´m pretty new to React and Redux and have some issue during my first steps with it. I tried to follow the examples in the Redux Doc´s, but it´s hard for me to understand everything, because every example is jumping between ES5 - 6 or even 7 syntax.

However, When I try to dispatch an action I got the following error

Uncaught TypeError: (0 , _index2.default) is not a function

Error Message

I know that SO Community doesn´t prefer so much code within one Question, but I don´t know where the problem is coming from. Sorry for that!

These is my Code:

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')
 )

My Store

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
 }

My Container Component

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)

My Child Component:

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>
    )
   }
  }

The Reducer

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

And Finally the action

export function addItem(item){
   console.log("addTOdo")
   return {
      type: 'ADD_ITEM',
      item
    }
  }

I hope someone can help me here, sitting since several hours and don´t understand what is happening.

like image 437
BayLife Avatar asked Feb 06 '23 21:02

BayLife


1 Answers

You are not exporting action creator as default. You need either

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

or

import {addItem} from '../actions/index'
like image 159
Yury Tarabanko Avatar answered Feb 11 '23 00:02

Yury Tarabanko