I'm working on an application using react and redux. I use api.
application flow:
- fill the form,
- click the send button,
- send data from the form to the api,
- go to the recipes page
The first component is the form to which you enter information (name, calories, type of diet).
class FormPage extends Component {
constructor(props) {
super(props);
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.goToListOfMealPage = this.goToListOfMealPage.bind(this);
}
handleFormSubmit(data) {
const name = data.name;
const calories = data.caloreis;
const diet = data.diet;
const health = data.health;
console.log(name)
return loadData( name, calories, diet, health)()
.then(({ error }) => {
if (!error) {
setTimeout(this.goToListOfMealPage, 1500);
}
return error;
}
);
}
goToListOfMealPage() {
const { history } = this.props;
history.push('/ListMeal');
}
render() {
return (
<Form onSubmit={this.handleFormSubmit}/>
);
}
}
const mapDispatchToProps = (dispatch) => {
return {
loadData: () => dispatch(loadData())
}
};
FormPage = connect(mapDispatchToProps)(FormPage)
export default FormPage;
handleFromSubmit function is to send form data to the api link (https://api.edamam.com/search?q=${name}n&app_id=${key.id}&app_key=${key.key}&calories=${calories}&health=${health}&diet=${diet}).
After filling in the form and after clicking the send button, I want to have a list of meals (recipes) on the new subpage.
where loadData is
const fetchDataStart = () => ({
type: actionTypes.FETCH_DATA_START,
});
const fetchDataSucces = (data) => ({
type: actionTypes.FETCH_DATA_SUCCESS,
data,
});
const fetchDataFail = () => ({
type: actionTypes.FETCH_DATA_FAIL,
});
const loadData = (name, calories, diet, health) => (dispatch) => {
dispatch(fetchDataStart());
return axios.get(`https://api.edamam.com/search?q=${name}n&app_id=${key.id}&app_key=${key.key}&calories=${calories}&health=${health}&diet=${diet}`)
.then(({ data }) => console.log(data) || dispatch(fetchDataSucces(data)))
.catch((err) => dispatch(fetchDataFail(err.response.data)));
};
After sending the form, I get an error TypeError: dispatch is not a function
I can not find the reason for this error

mapDispatchToPropsas well?loadData(name, calories, diet, health)()- currently dispatch isundefined