Not sure why redux-thunk is not working fine it says rentals is undefined while we correctly import and export it.
if I do normal call without dispatch it does work fine without any issue. is redux-thunk causing issue?
Please guide the same
Reducer.js
export const houseReducer = (state = [], action) => {
switch (action.type) {
case FETCH_HOUSE_SUCCESS:
return { ...state, data: action.rentals };
default:
return state;
}
};
Component
import * as action from "../actions";
function House({ props }) {
const dispatch = useDispatch();
useEffect(() => {
const { rentals } = dispatch(action.getHouseSuccess()); //undefined
}, []);
function mapStateToProps(state) {
return {
rentals: state.rentals,
};
}
export default connect(mapStateToProps)(House);
Action
export const getHouseSuccess = () => {
return function (dispatch) {
setTimeout(() => {
dispatch({
type: FETCH_HOUSE_SUCCESS,
rentals,
});
}, 1000);
};
};
This below statement working fine setTimeout causing issue ? it is calling before render ?
export const getHouseSuccess = () => {
return {
type: FETCH_HOUSE_SUCCESS,
rentals,
};
};