Created
April 1, 2022 15:39
-
-
Save Andaeiii/69d91e81f5988831603651af4ff5524c to your computer and use it in GitHub Desktop.
The basic Redux Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//on the index.js - import required libraries.. | |
import { Provider } from 'react-redux'; | |
import { createStore, applyMiddleware, compose } from 'redux'; | |
import thunk from 'redux-thunk'; | |
import reducers from './reducers/index'; //all reducers combined here.. | |
const store = createStore(reducers, compose(applyMiddleware(thunk))); | |
// also you refactor the initaal ReactDom.render script to this... | |
ReactDOM.render( // | |
<Provider store={store}> | |
<App /> | |
</Provider>, | |
document.getElementById('root') | |
); | |
//then in the reducers/index.js file.. | |
import { combineReducers } from 'redux'; | |
import posts from './posts'; | |
import loader from './loader'; | |
export default combineReducers({ posts, loader }); | |
//with this you are able to use | |
const state = useSelector(state => state); //get the global redux state.. | |
cosnt dispatch = useDispatch(); //get the global dispatch function. | |
//and also if incase you want to use the store outside redux.. | |
//lets say you want to dispatch an event outside the default scope... | |
// in index.js - export the store function... | |
export const store = createStore(reducers, compose(applyMiddleware(thunk))); | |
//then import it into whatever external file you intend to use it.. | |
import { store } from '../index.js'; // where index.js is the defualt react file.. | |
//then dispatch events from there... | |
store.dispatch({type:'LOADING_COMPLETE', payload:req.data}) | |
//and boom you are good to go.. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment