5

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Glad to have helped :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.