I have the following Search Component
import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { search } from '../../actions/actions'
class Search extends Component {
render() {
const {handleSubmit, "fields":{query}} = this.props;
return (
<div>
<form onSubmit={handleSubmit(search)}>
<Field className='searchInput' name="query" component="input" type="text" placeholder="Enter course name"/>
<button className='searchSubmit' type='submit' >Search</button>
</form>
</div>
);
}
}
export default reduxForm({
"form":"searchForm",
"fields":["query"]
}, null, {search})(Search);
My search Action is like so
search : function(query){
var data = {
ep:"EP_SEARCH",
payload:{
query: query.query
}
}
getAsynch(data).then(function(result){
return {type: GET_DATA, payload:result};
})
}
I get a response from the endpoint with a result but I need to somehow dispatch the action.
I tried instead of
getAsynch(data).then(function(result){
return {type: GET_DATA, payload:result};
})
this
return function(dispatch){
getAsynch(data).then(function(result){
dispatch({type: GET_DATA, payload:result});
})
}
but it throws an error that dispatch is not defined. Probably because I'm not passing it anywhere in the Component.
I'm using Redux Thunk, and my actions work properly for everything except ReduxForm. After reading a lot on the topic if discovered the solution.
More info on the topic
http://redux-form.com/6.2.0/docs/api/ReduxForm.md/
TBH I still don't get how dispatch got inside the action creator and why the previous suggestions, which I tried, do not work. A comment from someone more familiar with the topic would be nice.