The snippets in this page work with the Atoti UI starter. If you have not
installed it yet, then read Setup.
Atoti uses Redux for state management. A basic
understanding of it is preferable in order to understand what follows.
react-redux. It works just like when you use Redux to manage the state of any web application. The only difference is that you do not have to create and provide a store, as Atoti UI does it for you.
See more details and examples below.
Consume state attributes
To retrieve attributes from the state, use useSelector. For instance, the following snippet implements a higher order component which depends onisPresenting. If it is true, then it just displays the application, but if it is false, then it also displays a custom horizontal application bar to the left of it.
index.tsx:
getIsPresenting in the example above, more selectors are exported from @activeviam/atoti-ui-sdk and allow you to retrieve specific pieces of state:
- getActiveToolKey
- getDashboardState
- getIsDeferred
- getIsPresenting
- getHasUnsavedChanges
- getSelectedLeafKey
Trigger state updates
To update the state, use useDispatch. For instance, the following snippet implements an application menu item which adds a new empty page to the dashboard each time the user clicks it.index.tsx:
Advanced use cases
For more advanced use cases such as extending the state or changing the initial state, you can use a tool called a store enhancer. Store enhancers allow to hook into the application’s Redux store creation. They can be passed through Configuration, in yourindex.tsx:
Extend the state
What if you need to share state (that is not part of the core Atoti UI state) between several components? With the right store enhancer, your own state can live along the core state, within the Atoti UI store. For instance, the following store enhancer introduces a boolean attribute calledfoo along with an action to toggle it. It allows your components to use useSelector and useDispatch in order to consume and update this slice of state.