# Local Store

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:

```typescript
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`:

```typescript
export class CounterStore extends Store {
    state = {
        counter: 0
    }

    increment = () => {
        this.setState(({ counter }) => ({ counter: counter + 1 }))        
    }
    
    decrement = () => {
        this.setState({ counter }) => ({ counter: counter - 1 }))
    }
}

@Inject({
    counterStore: CounterStore
})
export class Counter extends React.Component {
    get counterStore() {
        return this.props.counterStore
    }

    render() {
        return (
            <div>
                <p>{this.counterStore.state.counter}</p>
                <button onClick={this.counterStore.decrement}>Decrement</button>
                <button onClick={this.counterStore.increment}>Increment</button>
            </div>
        )
    }
}
```

## 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:

```typescript
ReactDOM.render(
    <Deviation providers={[TotalSumStore]}>
        <AppRoot />
    </Deviation>,
    document.getElementById('root')
)
```

Then you can inject it into the local one:

```typescript
@Inject({
    totalSumStore: TotalSumStore
})
export class CounterStore extends Store { }
```

We also need to control the `TotalSumStore` via the local one:

```typescript
export class CounterStore extends Store {
    get totalSumStore() {
        return this.props.localSumStore
    }
    
    increment = () => {
        this.setState(({ counter }) => ({ counter: counter + 1 }))
        this.totalSumStore.increment()
    }
    
    decrement = () => {
        this.setState(({ counter }) => ({ counter: counter - 1 }))
        this.totalSumStore.decrement()
    }
    
    storeWillUnmount() {
        this.localSumStore.cutdown(this.state.counter)
    }
}
```

## 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:

```typescript
export class AppRoot extends React.Component {
    render() {
        const extraProps = { initialState: 5 }
    
        return (
            <Deviation providers={[CounterStore]} {...extraProps}>
                <Counter />
            </Deviation>
        )
    }
}
```

Then, we can access these extra props directly via store's props

```typescript
export class CounterStore extends Store {
    state = {
        counter: this.props.initialState
    }
    
    storeDidMount() {
        this.props.totalSumStore.add(this.state.counter)
    }
}
```
