Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/rjz/typescript-react-reducer-component

A minimal React state container, written in TypeScript
https://github.com/rjz/typescript-react-reducer-component

Last synced: 17 days ago
JSON representation

A minimal React state container, written in TypeScript

Awesome Lists containing this project

README

        

# TypeScript React Reducer Components

A minimal state container inspired by Reason's [state reducers][reason-reducer],
adapted for TypeScript.

## Usage

Using the example factory in [`src/reducerComponent`](src/reducerComponent),
creating new reducer components is as easy as 1, 2, 3...

1. Declare `State` and [`Action`][ts-actions] types for the component.

```typescript
type Action =
| 'INCREMENT'
| 'DECREMENT';

type State = {
count: number,
};
```

2. `make` the component from a reducer and a rendering function.

```typescript
import * as RC from './reducerComponent';

const Counter: RC.Component = ({ count, send }) => (


Count: {count}



send('INCREMENT')}>+1
send('DECREMENT')}>-1


);

const reduce: RC.Reducer = (state, action) => {
switch (action) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
};

export default RC.make(reduce)(Counter);
```

3. Profit

## Example

```sh
$ yarn
$ yarn start
```

## License

ISC

[ts-actions]: https://rjzaworski.com/2016/09/typescript-redux-async-actions
[reason-reducer]: https://github.com/reasonml/reason-react/blob/master/docs/state-actions-reducer.md