We have several components that allows you to store information locally. You can use something like the value component to store information, but you can also have your own JavaScript logic.
Here the counter example from Redux. In the subscribe you just set the state in the global scope from App Connect. You can then have an expression like {{counter.value}}
which will automatically update when the state changes.
Example uses the Redux UMD package.
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case 'counter/incremented':
return { value: state.value + 1 }
case 'counter/decremented':
return { value: state.value - 1 }
default:
return state
}
}
// Create a Redux store for the state
let store = Redux.createStore(counterReducer)
// Put the state in the App Connect global scope
// The state is now everywhere in the app
// via expressions under `counter` available
dmx.global.set('counter', store.getState())
// Subscribe to updates
store.subscribe(() => {
dmx.global.set('counter', store.getState())
})
// we can now update the state using the dispatch
store.dispatch({ type: 'counter/incremented' })