REDUX in 4 STEPS
- Store requires: Reducer & State
- Reducer requires: State & Action
- Subscribe - Listens for the store to change
- Dispatch action - Do the action
import React, { Component } from "react";
import { createStore } from "redux";
class ReduxDemo extends Component {
render() {
const reducer = (state, action) => {
if (action.type === "attack") {
return action.payload;
}
if (action.type === "greenattack") {
return action.payload;
}
return state;
};
const store = createStore(reducer, "Peace");
store.subscribe(() => {
console.log("Store is now", store.getState());
});
store.dispatch({ type: "attack", payload: "Iron Man" });
store.dispatch({ type: "greenattack", payload: "Hulk" });
return <div>Hello</div>;
}
}
export default ReduxDemo;