I am trying to set the user's current location in state using redux, but whenever I try I keep getting the error Actions must be plain objects. Use custom middleware for async actions. Everywhere I look it states this is an error of not having thunk, but as you can see from my store.js below I have applyMiddleware(thunk) in my store.
Here are all of my files associated with this action:
store.js
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const initialState = {};
const composeEnhancers = window._REDUX_DEVTOOLS_EXTENSION_COMPOSE_ || compose;
const store = createStore(
rootReducer,
initialState,
composeEnhancers(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__() : f => f
)
);
export default store;
locationAction.js
export const setLocationState = () => {
dispatch => {
navigator.geolocation.getCurrentPosition((position) => {
dispatch({
type: SET_LOCATION,
payload: position
});
});
}
}
Note: Even when I got rid of the dispatch and just had the setLocationState() function return type: SET_LOCATION I would get the same error, so that's leading me to believe that I may have configured something incorrectly somewhere.
locationReducer.js
const initialState = {
latitude: 0,
longitude: 0
}
export default (state = initialState, action) => {
switch(action.type) {
case SET_LOCATION:
return {
...state,
position: action.payload
}
default:
return state
}
}
index.jsx
import { setLocationState } from '../../../actions/locationActions'
const locationState = useSelector(state => state.location);
const dispatch = useDispatch();
dispatch(setLocationState()) // <- Here is where error occurs