Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/reactions/component

Declarative version of React.Component
https://github.com/reactions/component

Last synced: about 1 month ago
JSON representation

Declarative version of React.Component

Awesome Lists containing this project

README

        

# Reactions Component

This has moved to [Reach UI](https://ui.reach.tech/component-component), but this repo is here for the sake of history I guess.

## What?

Declarative version of React.Component.

## Why?

Because sometimes you want a lifecycle or some state but don't want to create a new component. Also, this stuff is composable as heck.

## Installation

```bash
npm install @reactions/component
# or
yarn add @reactions/component
```

And then import it:

```js
// using es modules
import Component from "@reactions/component";

// common.js
const Component = require("@reactions/component");

// AMD
// I've forgotten but it should work.
```

Or use script tags and globals.

```html

```

And then grab it off the global like so:

```js
const Component = ReactionsComponent;
```

## How?

Let's say you want some async data but don't want to make a whole new component just for the lifecycles to get it:

```render-babel
// import Component from '@reactions/component'
const Component = ReactComponentComponent;

ReactDOM.render(


Let's get some gists!


{
fetch("https://api.github.com/gists")
.then(res => res.json())
.then(gists => setState({ gists }));
}}
>
{({ state }) =>
state.gists ? (

    {state.gists.map(gist => (
  • {gist.description}

  • ))}

) : (
Loading...

)
}

,
DOM_NODE
);
```

Or maybe you need a little bit of state but an entire component
seems a bit heavy:

```render-babel
// import Component from '@reactions/component'
const Component = ReactComponentComponent;

ReactDOM.render(

{({ setState, state }) => (


Every app needs a counter!



setState(state => ({ count: state.count - 1 }))
}
>
-

{state.count}

setState(state => ({ count: state.count + 1 }))
}
>
+


)}
,
DOM_NODE
);
```

## Props

You know all of these already:

* `didMount({ state, setState, props, forceUpdate })`
* `shouldUpdate({ state, props, nextProps, nextState })`
* `didUpdate({ state, setState, props, forceUpdate, prevProps, prevState })`
* `willUnmount({ state, props })`
* `children({ state, setState, props, forceUpdate })`
* `render({ state, setState, props, forceUpdate })`

## Legal

Released under MIT license.

Copyright © 2017-present Ryan Florence