Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: (0 , _reactRedux.bindActionCreators) is not a functin

Tags:

reactjs

redux

I'm playing around with redux, I'm and started getting this error, can't resolve it, I was hoping it has any meaning, but can't seem to find any information.. Can anyone explain the reason this may show? Thanks..

Code:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'react-redux';
import  { searchForBeers } from '../../actions/index';


class SearchBar extends Component {

  constructor(props) {
    super(props);

    this.state = { term: ''}

    this.onFieldChange = this.onFieldChange.bind(this);
    this.onClickSearch = this.onClickSearch.bind(this);
  }

  onFieldChange(event) {
    this.setState({ term: event.target.value })

    console.log(this.state)
  }

  onClickSearch() {
    console.log(this.state)

    this.props.searchForBeers(this.state.term)
  }

  render() {
    return (
        <div className="col-lg-6" style={{top:20 , width:'70%'}}>
           <div className="input-group">
         <span className="input-group-btn">
           <button onClick={this.onClickSearch} className="btn btn-secondary" type="button">Go!</button>
         </span>
         <input type="text" className="form-control" placeholder="Search for..." onChange={this.onFieldChange} />
       </div>
       </div>
    )
  }

}

//Container functions

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ searchForBeers }, dispatch);
}

export default connect(null, mapDispatchToProps)(SearchBar);

Package json:

{
  "name": "redux-simple-starter",
  "version": "1.0.0",
  "description": "Simple starter package for Redux with React and Babel support",
  "main": "index.js",
  "repository": "[email protected]:StephenGrider/ReduxSimpleStarter.git",
  "scripts": {
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "css-loader": "^0.25.0",
    "jquery": "^2.2.1",
    "jsdom": "^8.1.0",
    "mocha": "^2.4.5",
    "node-sass": "^3.10.0",
    "react-addons-test-utils": "^0.14.7",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "axios": "^0.13.1",
    "babel-preset-stage-1": "^6.1.18",
    "lodash": "^3.10.1",
    "material-ui": "^0.15.4",
    "raw-loader": "^0.5.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^4.4.5",
    "react-router": "^2.0.1",
    "redux": "^3.5.2",
    "redux-promise": "^0.5.0",
    "redux-simple-promise": "^2.0.2"
  }
}
like image 350
MCMatan Avatar asked Sep 23 '16 07:09

MCMatan


People also ask

Can I connect functional component to Redux?

As of version 7.1, Redux supports React Hooks, meaning you can use Redux with Hooks in your functional components instead of using Redux connect() .

What is bindActionCreators in Redux?

bindActionCreators(actionCreators, dispatch) Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch call so they may be invoked directly. Normally you should just call dispatch directly on your Store instance.

What is mapStateToProps and mapDispatchToProps?

The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.

What does mapStateToProps do?

As the first argument passed in to connect , mapStateToProps is used for selecting the part of the data from the store that the connected component needs. It's frequently referred to as just mapState for short. It is called every time the store state changes.


1 Answers

It's the redux module that provides the bindActionCreators function so the correct way to import it would be

import { bindActionCreators } from 'redux';

instead of importing it from react-redux.

There is a page with examples in the docs over at their API documentation site.

The error you're seeing simply looks funky because of the way babel transpiles ES2015 modules but it's not actually React or Redux that's throwing it.

like image 196
ivarni Avatar answered Nov 03 '22 00:11

ivarni