Doing it with hooks. I ran into a similar problem, but I was using react-redux with hooks. I did not want to lard up my interface code (i.e., react components) with lots of code dedicated to retrieving/sending information from/to the store. Rather, I wanted functions with generic names to retrieve and update the data. My path was to put the app's
const store = createSore(
allReducers,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
into a module named store.js
and adding export
before const
and adding the usual react-redux imports in the store.js. file. Then, I imported to index.js
at the app level, which I then imported into index.js with the usual import {store} from "./store.js"
The child components then accessed the store using the useSelector()
and useDispatch()
hooks.
To access the store in non-component front end code, I used the analogous import (i.e., import {store} from "../../store.js"
) and then used store.getState()
and store.dispatch({*action goes here*})
to handled retrieving and updating (er, sending actions to) the store.