- Redux basics soon
- Redux simplified
- React online shop soon
- Actions
- Reducers
- Store
- connect
- First step Actions in src you will create Directory actions and inside of it index.js
const yourAction = (value) => {
return {
type: "ACTION_NAME",
payload: value
};
};
- Second step Reducers
in src you will create Directory reducers and inside of it index.js
const allReducers = (state, action) => {
switch (action.type) {
case "ACTION_NAME":
return {
...state,
// any other modifications you need to do
};
default:
return state;
}
};
export default allReducers;
- Third step creating the store
in src directory in index.js you should add
import { createStore } from "redux";
import { Provider } from "react-redux";
import allReducers from "./reducers";
// creating the store
const store = createStore(
allReducers,
{
// any initial values for your store
},
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
// adding the Provider wrapper and sending the store variable
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
- the last step connect
in any component you need to access the store
first import
import { connect } from "react-redux";
import { // any action you want to dispatch in this component } from "../actions";
then send the state and the action the this component through props using this functions
const mapStateToProps = (store) => {
// any info you want to pass to this component
};
const mapDispatchToProps = {
// any actions you need to pass to this component
};
Lastly you need to connect Redux with that component
export default connect(mapStateToProps, mapDispatchToProps)(YourComponent);
Happy codding 😎