I am trying to get redux working with my application, but there is the problem with me getting error: Actions must be plain objects. Use custom middleware for async actions.
I am new to redux, as I saw the problem is usually because people did not use applyMiddlewear, but I did it and do not understand why my code keeps getting this mistake.
My action that is probably getting mistake:
export function wordsAreFetching(bool){
return{
type: 'WORDS_ARE_FETCHING',
areFetching: bool
}
export function wordsFetchData(parsed) {
return (dispatch) => {
dispatch(wordsAreFetching(true));
fetch('APICALL(here is url actually)', {
method: "POST",
headers: {
"Content-type": "application/json"
},body: JSON.stringify({
words: parsed
})
})
.then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}
dispatch(wordsAreFetching(false));
return response;
})
.then((response) => response.json())
.then((items) => dispatch(wordsFetchDataSuccess(items)))
.catch(() => dispatch(wordsHasErrored(true)));
};
console.log(this.props.items)
}
My combine reducers file:
export default combineReducers({
word,
wordsAreFetching,
wordsFetchHasErrored
});
My store creation:
export default function configureStore(initialState) {
return createStore(
rootReducer,
initialState,
applyMiddleware(thunk)
);
}
How I call:
const mapStateToProps = (state) => {
return {
items: state.items,
hasErrored: state.wordsFetchHasErrored,
areFetching: state.wordsAreFetching
};
};
const mapDispatchToProps = (dispatch) => {
return {
fetchData: (parse) => dispatch(wordsFetchData(parse))
};
};
componentDidMount = () => {
this.props.fetchData(this.state.filterArray);
}