Skip to content

Commit d043853

Browse files
authored
Switch to RTK (Redux ToolKit). Fixes #4
1 parent 4403165 commit d043853

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

index.html

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<script src="https://unpkg.com/redux@4.0.5/dist/redux.js"></script>
1717
<script src="https://unpkg.com/react-redux@7.2.0/dist/react-redux.js"></script>
1818

19+
<script src="https://unpkg.com/@reduxjs/toolkit@1.9.5/dist/redux-toolkit.umd.min.js"></script>
20+
1921
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
2022
</head>
2123
<body class="m-2">
@@ -96,6 +98,15 @@
9698
bonus: loadedBonus = 5
9799
} = loaded;
98100

101+
const modalReducerInitialState = {
102+
showModal: false,
103+
bonus: loadedBonus
104+
};
105+
106+
107+
108+
//// Redux standalone... (aka before Redux Tooklit)
109+
/*
99110
// Action Types
100111
const SHOW_MODAL = "SHOW_MODAL";
101112
const HIDE_MODAL = "HIDE_MODAL";
@@ -109,7 +120,7 @@
109120
// Reducers
110121
const modalReducer = (state, action) => {
111122
112-
state ??= { showModal: false, bonus: loadedBonus };
123+
state ??= modalReducerInitialState;
113124
114125
switch(action.type) {
115126
case SHOW_MODAL:
@@ -127,6 +138,38 @@
127138
};
128139
129140
const reduxStore = Redux.createStore(modalReducer);
141+
*/
142+
143+
//// Redux Toolkit...
144+
/**/
145+
const { createSlice, configureStore } = RTK;
146+
147+
// Create slice
148+
const modalSlice = createSlice({
149+
name: "modal",
150+
initialState: modalReducerInitialState,
151+
reducers: {
152+
showBonusModal: (state) => {
153+
state.showModal = true;
154+
},
155+
hideBonusModal: (state) => {
156+
state.showModal = false;
157+
},
158+
setBonus: (state, action) => {
159+
state.bonus = action.payload;
160+
}
161+
}
162+
});
163+
164+
// Reducers and actions generated from the slice
165+
const { showBonusModal, hideBonusModal, setBonus } = modalSlice.actions;
166+
167+
// Create store with Redux Toolkit "configureStore"
168+
const reduxStore = configureStore( {
169+
reducer: modalSlice.reducer // simple scenario; therefore don't need to separate into namespaces
170+
// reducer: { modal: modalSlice.reducer } // "complex scenario" would need to establish separate namespace for modal
171+
} );
172+
/**/
130173

131174

132175

@@ -136,11 +179,16 @@
136179
loadedPlaces.some(Boolean) ? loadedPlaces : EXAMPLE_DATA
137180
);
138181

139-
const dispatch = ReactRedux.useDispatch();
140-
141-
const showModalState = ReactRedux.useSelector(state => state.showModal);
182+
const { useDispatch, useSelector } = ReactRedux;
183+
const dispatch = useDispatch();
142184

143-
const CL_PERCENT = ReactRedux.useSelector(state => state.bonus);
185+
//// Redux standalone... (aka beforeRedux Tooklit)
186+
const showModalState = useSelector(state => state.showModal);
187+
const CL_PERCENT = useSelector(state => state.bonus);
188+
//// Redux Toolkit...
189+
// unchanged since .configureStore() { reducer: modalSlice.reducer } NOT { reducer: { <namespace>: modalSlice.reducer } }
190+
// below needed ONLY if configureStore() WITH reducer: { modal: modalSlice.reducer } NOT reducer: modalSlice.reducer
191+
// const { bonus: CL_PERCENT, showModal: showModalState } = useSelector(state => state.modal); // ONLY if "complex scenario"
144192

145193
const handleBonusClick = () => {
146194
dispatch( showBonusModal() );

0 commit comments

Comments
 (0)