|
16 | 16 | <script src="https://unpkg.com/redux@4.0.5/dist/redux.js"></script>
|
17 | 17 | <script src="https://unpkg.com/react-redux@7.2.0/dist/react-redux.js"></script>
|
18 | 18 |
|
| 19 | +<script src="https://unpkg.com/@reduxjs/toolkit@1.9.5/dist/redux-toolkit.umd.min.js"></script> |
| 20 | + |
19 | 21 | <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
20 | 22 | </head>
|
21 | 23 | <body class="m-2">
|
|
96 | 98 | bonus: loadedBonus = 5
|
97 | 99 | } = loaded;
|
98 | 100 |
|
| 101 | +const modalReducerInitialState = { |
| 102 | + showModal: false, |
| 103 | + bonus: loadedBonus |
| 104 | +}; |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +//// Redux standalone... (aka before Redux Tooklit) |
| 109 | +/* |
99 | 110 | // Action Types
|
100 | 111 | const SHOW_MODAL = "SHOW_MODAL";
|
101 | 112 | const HIDE_MODAL = "HIDE_MODAL";
|
|
109 | 120 | // Reducers
|
110 | 121 | const modalReducer = (state, action) => {
|
111 | 122 |
|
112 |
| - state ??= { showModal: false, bonus: loadedBonus }; |
| 123 | + state ??= modalReducerInitialState; |
113 | 124 |
|
114 | 125 | switch(action.type) {
|
115 | 126 | case SHOW_MODAL:
|
|
127 | 138 | };
|
128 | 139 |
|
129 | 140 | 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 | +/**/ |
130 | 173 |
|
131 | 174 |
|
132 | 175 |
|
|
136 | 179 | loadedPlaces.some(Boolean) ? loadedPlaces : EXAMPLE_DATA
|
137 | 180 | );
|
138 | 181 |
|
139 |
| - const dispatch = ReactRedux.useDispatch(); |
140 |
| - |
141 |
| - const showModalState = ReactRedux.useSelector(state => state.showModal); |
| 182 | + const { useDispatch, useSelector } = ReactRedux; |
| 183 | + const dispatch = useDispatch(); |
142 | 184 |
|
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" |
144 | 192 |
|
145 | 193 | const handleBonusClick = () => {
|
146 | 194 | dispatch( showBonusModal() );
|
|
0 commit comments