user1637909
user1637909

Reputation: 173

Cannot read property 'dispatch' of undefined'. React

Following is the code that I am using. I am getting an error like Uncaught TypeError: Cannot read property 'dispatch' of undefined. I also want to dispatch an action.

import { connect } from 'react-redux'
let SearchBar = ({ dispatch }) => {
    let input
    return (
       <div>
           <form>
              <input type="text" placeholder="Search"  />
              <p>
                 <input type="checkbox" />
                 {' '}
                 Free
              </p>                
           </form>
       </div>
   )
}

SearchBar = connect()(SearchBar)
export default SearchBar()

Upvotes: 5

Views: 21522

Answers (3)

EDISON J
EDISON J

Reputation: 344

We can add mapStateToProps inside the connect also.

const mapStateToProps = state => ({
  uData: state
});

export default connect(mapStateToProps)(App);

We do not need to give null inside the connect().

I believe it may help some of you.

Upvotes: 0

Shubham Khatri
Shubham Khatri

Reputation: 281626

You are right in passing and retreiving the dispatch from connect. As connect without any parameters will return only the dispatch

However the problem is in the way you are exporting the component

export default SearchBar();

You don't need () after SearchBar and one more thing. For clarity you can use a different name like

var search = connect()(SearchBar)
export default search;

or you can do it in a single step like

export default connect()(SearcBar);

The later is a shorthand for the same thing as the former.

Upvotes: 1

Drum
Drum

Reputation: 507

A couple of things I notice immediately with your code example:

First of all the connect function requires some parameters in the first parenthesis, even if that parameter is a null, also I don't think you need the parentheses on the export line. Try replacing those last two lines with something like this:

export default connect(null)(SearchBar);

see if that makes any difference.

Upvotes: 2

Related Questions