This repository was archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
This repository was archived by the owner on Apr 27, 2024. It is now read-only.
Add SideEffectors #3
Copy link
Copy link
Open
Description
SideEffectors are objects or systems that react to actions, but are allowed to produce side effects. Unlike reducers, SideEffectors cannot modify state directly, however they can dispatch actions.
SideEffectors should have two method "hooks" into the Store:
PreEffect: called immediately when an action is dispatched, before reduction.PostEffect: called after reduction.
Each hook is passed the action that was dispatched, the current state, and an IActionDispatcher.
Architecturally, SideEffectors reduce burden on components by being responsible for side effects, especially asynchrony. For example, a component doesn't need to care about the details of requesting a remote resource, it just needs to care about displaying it.
Here's an example flow for the use case where clicking a button should load a remote image and display it in a component:
- Button is clicked and dispatches a
ButtonClickedaction. - A SideEffector hooks on the
ButtonClickedaction and initiates a web request for the resource. - Reducer takes the
ButtonClickedaction and updates state to indicate that the image component is in the 'loading' state. - The image component observes the 'loading' state and displays a loading icon.
- The SideEffector receives a response with the requested resource and dispatches another action:
ImageLoaded, with the image included. - Reducer takes the
ImageLoadedaction and updates the image component state to 'loaded' and adds the image to the state. - The image component observes the 'loaded' state, gets the image, and displays it.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels