Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fabiospampinato/unstated-compose

Compose multiple containers into one.
https://github.com/fabiospampinato/unstated-compose

compose containers react unstated

Last synced: about 1 month ago
JSON representation

Compose multiple containers into one.

Awesome Lists containing this project

README

        

# Unstated Compose

Compose multiple containers into one.

This is useful when you want to have a single container, perhaps subscribed to via [unstated-connect2](https://github.com/fabiospampinato/unstated-connect2), but it starts to become too big. This package allows you to refactor it into multiple separate containers that can still be merged back together.

## Install

```sh
npm install --save unstated-compose
```

## Usage

It allows to write an app like the following, where there are multiple containers, which can access each other's methods and state, while your components still consume only 1 container.

```tsx
/* IMPORT */

import * as React from 'react';
import {render} from 'react-dom';
import {Provider, Subscribe} from 'unstated';
import {compose, ChildContainer, ParentContainer} from 'unstated-compose';

/* CONTAINERS */

class CounterContainer extends ChildContainer {
state = {
counter: 123
}
get = () => {
return this.state.counter;
}
set = ( counter ) => {
this.setState ({ counter });
}
setMessage = () => {
this.ctx.message.set ( 'Hello from the CounterContainer' );
}
setJoke = () => {
this.ctx.setJoke ( 'Hello from the CounterContainer' );
}
}

class MessageContainer extends ChildContainer {
state = {
message: 'Default message'
}
get = () => {
return this.state.message;
}
set = ( message ) => {
this.setState ({ message });
}
setCounter = () => {
this.ctx.counter.set ( 'Hello from the MessageContainer' );
}
setJoke = () => {
this.ctx.setJoke ( 'Hello from the MessageContainer' );
}
}

@compose ({
counter: CounterContainer,
message: MessageContainer
})
class AppContainer extends ParentContainer {
state = {
joke: 'Default joke'
}
get = () => {
return this.state.joke;
}
setJoke = ( joke ) => {
this.setState ({ joke });
}
setCounter = () => {
this.ctx.counter.set ( 'Hello from the AppContainer' );
}
setMessage = () => {
this.message.set ( 'Hello from the AppContainer' ); // `.ctx` can be omitted in the parent container
}
}

/* APP */

const App = () => (

{
app => (


Counter


{app.counter.get ()}


app.counter.set ( Math.random () )}>Update from CounterContainer
Update from MessageContainer
Update from AppContainer

Message


{app.state.message.message}


app.message.set ( `Now is: ${Date.now()}`)}>Update from MessageCounter
Update from CounterContainer
Update from AppContainer

Joke


{app.state.joke}


app.setJoke ( `Joke coming in ${Date.now ()}...` )}>Update from AppContainer
Update from CounterContainer
Update from MessageContainer

)
}

);

render (


,
document.getElementById ( 'app' )
);
```

## Related

- **[unstated-with-containers](https://github.com/fabiospampinato/unstated-with-containers)**: Higher-Order Component for subscribing to containers.
- **[unstated-connect2](https://github.com/fabiospampinato/unstated-connect2)**: Connect containers to components, without sacrificing performance.
- **[unstated-hmr](https://github.com/fabiospampinato/unstated-hmr)**: Preserve containers' states across Hot-Module-Replacements.
- **[unstated-compose-suspense](https://github.com/fabiospampinato/unstated-compose-suspense)**: Add suspend/unsuspend support to `unstated-compose`.
- **[unstated-compose-suspense-middleware](https://github.com/fabiospampinato/unstated-compose-suspense-middleware)**: Add middlewares support to `unstated-compose-suspense`.
- **[unstated-suspense](https://github.com/fabiospampinato/unstated-suspense)**: Suspend/unsuspend updates propagation from your containers.
- **[unstated-suspense-middleware](https://github.com/fabiospampinato/unstated-suspense-middleware)**: Add middlewares support to `unstated-suspense`.

## License

MIT © Fabio Spampinato