* Dispatches an action. It is the only way to trigger a state change. * * The `reducer` function, used to create the store, will be called with the * current state tree and the given `action`. Its return value will * be considered the **next** state of the tree, and the change listeners
(action: A)
| 269 | * return something else (for example, a Promise you can await). |
| 270 | */ |
| 271 | function dispatch(action: A) { |
| 272 | if (!isPlainObject(action)) { |
| 273 | throw new Error( |
| 274 | `Actions must be plain objects. Instead, the actual type was: '${kindOf( |
| 275 | action |
| 276 | )}class="st">'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https:class="cm">//redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.` |
| 277 | ) |
| 278 | } |
| 279 | |
| 280 | if (typeof action.type === class="st">'undefined') { |
| 281 | throw new Error( |
| 282 | class="st">'Actions may not have an undefined "type" property. You may have misspelled an action type string constant.' |
| 283 | ) |
| 284 | } |
| 285 | |
| 286 | if (typeof action.type !== class="st">'string') { |
| 287 | throw new Error( |
| 288 | `Action class="st">"type" property must be a string. Instead, the actual type was: '${kindOf( |
| 289 | action.type |
| 290 | )}class="st">'. Value was: '${action.type}' (stringified)` |
| 291 | ) |
| 292 | } |
| 293 | |
| 294 | if (isDispatching) { |
| 295 | throw new Error(class="st">'Reducers may not dispatch actions.') |
| 296 | } |
| 297 | |
| 298 | try { |
| 299 | isDispatching = true |
| 300 | currentState = currentReducer(currentState, action) |
| 301 | } finally { |
| 302 | isDispatching = false |
| 303 | } |
| 304 | |
| 305 | const listeners = (currentListeners = nextListeners) |
| 306 | listeners.forEach(listener => { |
| 307 | listener() |
| 308 | }) |
| 309 | return action |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Replaces the reducer currently used by the store to calculate the state. |