Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Redux Dispatch Action from Component

I'm trying to dispatch an action from a button click.

I've created my component. I connect it to the store that is passed down to the component by a Provider. But I get an error:

Uncaught TypeError: this.doSearchClick is not a function

I have my imports:

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import action_doSearch from '../../actions/searchActions'

My component:

class SearchForm extends React.Component {
  constructor(props, doSearchClick) {
    super(props);
    this.search = this.search.bind(this);
    this.doSearchClick = doSearchClick;
  }

  search() {
    this.doSearchClick('bla bla from search');
  }

  render() {
    return(
      <div>
        <button onClick={this.search}>Search</button>
      </div>
    );
  }
}

Not sure if this is required:

SearchForm.propTypes = {
  doSearchClick: PropTypes.func.isRequired
};

Finally the connect stuff:

const mapStateToProps = (state) => {
  return {
    state
  }
};

const mapDispatchToEvents = (dispatch) => {
  return {
    doSearchClick: (searchCriteria) => {
      dispatch(action_doSearch(searchCriteria));
    }
  };
};
const SearchFormConnected = connect(
  mapStateToProps,
  mapDispatchToEvents
)(SearchForm);

At the top level I pass the store down via Provider:

import { Provider } from 'react-redux'

const finalCreateStore = compose(
   applyMiddleware(
      middleware,
      thunk
   ),
   DevTools.instrument()
)(createStore);

const store = finalCreateStore(reducer);

ReactDOM.render(
  <Provider store={store}>
    ....

I have also tried to achieve this by accessing the store via context (didn't work and might be deprecated) and also using the @connect attribute (gave me a similar error).

like image 985
Dave Avatar asked Feb 24 '16 12:02

Dave


People also ask

How can I call Redux action from functional component?

There are two ways to dispatch actions from functional components: Using mapDispatachToProps function with connect higher order component, same as in class based components. Using useDispatch hook provided by react-redux . If you want to use this hook, then you need to import it from the react-redux package.

How do you call Redux action outside component?

To trigger a Redux action from outside a component with React, we call store. dispatch from anywhere in our project. import { store } from "/path/to/createdStore"; function testAction(text) { return { type: "TEST_ACTION", text, }; } store.


2 Answers

doSearchClick will be passed in the props object. So you need to type props.doSearchClick to access it.

like image 149
Alex Moldovan Avatar answered Oct 01 '22 18:10

Alex Moldovan


This:

SearchForm.propTypes = {
   doSearchClick: PropTypes.func.isRequired
};

Means that your SearchForm component is expecting to get a function named doSearchClick as props from its father.

If the father passed this props the child should access it via

props.doSearchClick
like image 37
Sagi Medina Avatar answered Oct 01 '22 19:10

Sagi Medina