Help to understand what the problem is. When I write mapDispatchToProps like this:
const mapDispatchToProps = (dispatch: any) => {
return {
getPostByIdAction: (post: any) => dispatch ({type: GET_ID, payload: post})
}
};
everything is working fine. But when I try to dispatch the function in this way:
const mapDispatchToProps = (dispatch: any) => {
return {
getPostByIdAction: (post: any) => dispatch (getPostById (post))
}
};
I get an error: Actions must be plain objects. Use custom middleware for async actions. What could have gone wrong?
my actions:
export const getPostById = async (id: any) => {
const myResponse = await fetch (`https://jsonplaceholder.typicode.com/posts/$ {id}`);
const myJson = await myResponse.json ();
const post = myJson.body
}
my reducer:
import {combineReducers} from 'redux'
import {pageReducer} from './page'
export const rootReducer = combineReducers ({
page: pageReducer
})
import {GET_ID} from '../actions/PageActions'
const initialState = {
post: "Click on article to read it"
}
export function pageReducer (state = initialState, action: any) {
switch (action.type) {
case GET_ID:
return {... state, post: action.payload};
default:
return state
}
}