Local Store
This feature is not fully supported yet
Dependency Injection is the modal of shared state containers. In very rare cases that people will want to make store run locally. It's worth to say that in some Redux ecosystem store systems, container is overused sometimes. People usually use store to replace component state, a way to separate the business logic from component functionalities. We do not recommend to do so. However, in some cases when the business logic is fat, it's costly to switch between store and component architecture, then having local stores is essential.
Create a Multiple Counter App
For a multiple-counter app, first we need to have an AppRoot which store and render the counters. We will have a global TotalSumStore to manage the total sum:
export class TotalSumStore extends Store {
state = {
totalSum: 0
}
increment() {
this.setState(({ counter }) => ({ counter: counter + 1 }))
}
decrement() {
this.setState(({ counter }) => ({ counter: counter - 1 }))
}
cutdown(amount) {
this.setState(({ counter }) => ({ counter: counter - amount }))
}
}
@Inject({
totalSumStore: TotalSumStore
})
export class AppRoot extends React.Component {
get totalSumStore() {
return this.props.totalSumStore
}
state = {
numberOfCounters: 0
}
addCounter = () => {
this.setState(({ numberOfCounters }) =>
({ numberOfCounters: numberOfCounter + 1 }))
}
render() {
return (
<div>
<p> Total Sum: {this.totalSumStore.state.totalSum}<p>
<div>
{this.renderCounters}
<div>
<button onClick={this.addCounter}
</div>
)
}
renderCounters = () =>
Array
.from({
length: this.state.numberOfCounters
})
.map(() => (
<Deviation providers={[CounterStore]}>
<Counter />
</Deviation>
))
}
ReactDOM.render(
<Deviation providers={[TotalSumStore]}>
<AppRoot />
</Deviation>,
document.getElementById('root')
)Next, we have to create the CounterStore and the Counter component that we defined in our AppRoot:
Deviation Inheritance
Sometimes, we may want to connect the local store with the global store. Sometimes, we want to override the global store with the global one. Deviation give you a full control of how stores inheritance should work. When you defined a Deviation as a child of another Deviation, Deviation will automatically inherit the providers from its ancestor via React Context API. So that, you only have to define TotalSumStore at the global Deviation:
Then you can inject it into the local one:
We also need to control the TotalSumStore via the local one:
Extra Props
Store and component are different, component has state, props and render method. While store has only state. Its props has to be inject via Deviation. It's unpleasant if we want a store act like a component. Actually, store is shared between components. It's trivial that we passing props of an individual component to the store via its props. But there's another way that we can pass the props down to the store. It's via Deviation extra props. For example:
Then, we can access these extra props directly via store's props
Last updated
Was this helpful?