-
Notifications
You must be signed in to change notification settings - Fork 1
The Control Status Pattern
Often in client-side web development, the need arises in which distinct libraries with distinct event systems will have to be incorporated. Ideally, the application has a primary source of truth, while the other libraries follow the main application.
Coordination of the various parts of an application involves functionality to control secondary applications, the reporting of the status of those secondary applications, and a way to reconcile status with what is controlled.
In the case of React hooks, these tasks can be achieved with two custom React hooks. A Status state that represents the status of the application and Control state, that offers the ability to control secondary applications. Components are characterized by how they have access to the state. Components are either Control or Controlled components.
- uses the control state to control
- controlled with the control state
- report their status
- do not have access to control methods.
Reconciliation between what is being reported and the existing state of the application can be considered a side effect and reconciled with useEffect.
const initState = { center: [0, 0], zoom: 12 }
const statusReducer = ( state, action ) => {
switch(action.type) {
case 'center': return Object.assign({}, state, { center: action.payload }}
}}
const [ status, statusDispatcher ] = useReducer(statusReducer, initState)
const controlReducer = ( state, action ) => {
switch (action.type) {
case 'center' { return Object.assign({}, state, { center: action.payload })}
}
}
const [ control, controlDispatcher ] = useReducer(controlReducer, initState)
useEffect(() => {
controlDispatcher({type: 'center', payload: status.center})
}, [status.center])
<ControlA
controlDispatcher
/>
<ControlB
controlDispatcher
/>
<MapA
statusDispatcher
control
/>
<MapB
statusDispatcher
control
/>
<Info
status
/>