# @jbarzegar/component

> Declarative Version of React.Component

Latest version **2.0.2** (published 2018-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jbarzegar/component
pnpm add @jbarzegar/component
yarn add @jbarzegar/component
bun add @jbarzegar/component
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2018-08-14 |
| First published | 2018-08-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ryan Florence |
| Maintainers | stealtheritz |
| Keywords | react, component, react component |

## Links

- npm: https://www.npmjs.com/package/@jbarzegar/component
- Repository: https://github.com/jbarzegar/component
- Homepage: https://github.com/jbarzegar/component#readme
- Issues: https://github.com/jbarzegar/component/issues
- npm.io page: https://npm.io/package/@jbarzegar/component

## Dependencies (1)

- [lodash.curry](https://npm.io/package/lodash.curry.md) 4.1.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2018-08-14

## README

# Jbarzegar Component

A Fork of ![https://github.com/reactions/component](reactions/component)

## What?

Declarative version of React.Component.
Which also allows users to make "class methods"

## Why?

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

## Installation

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

And then import it:

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

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

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

Or use script tags and globals.

```html
<script src="https://unpkg.com/@jbarzegar/component"></script>
```

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:

```jsx
// import Component from '@jbarzegar/component'
const Component = ReactComponentComponent;

ReactDOM.render(
  <div>
    <h2>Let's get some gists!</h2>
    <Component
      initialState={{ gists: null }}
      didMount={({ setState }) => {
        fetch("https://api.github.com/gists")
          .then(res => res.json())
          .then(gists => setState({ gists }));
      }}
    >
      {({ state }) =>
        state.gists ? (
          <ul>
            {state.gists.map(gist => (
              <li key={gist.id}>{gist.description}</li>
            ))}
          </ul>
        ) : (
          <div>Loading...</div>
        )
      }
    </Component>
  </div>,
  DOM_NODE
);
```

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

```jsx
// import Component from '@reactions/component'
const Component = ReactComponentComponent;

ReactDOM.render(
  <Component initialState={{ count: 0 }}>
    {({ setState, state }) => (
      <div>
        <h2>Every app needs a counter!</h2>
        <button
          onClick={() =>
            setState(state => ({ count: state.count - 1 }))
          }
        >
          -
        </button>
        <span> {state.count} </span>
        <button
          onClick={() =>
            setState(state => ({ count: state.count + 1 }))
          }
        >
          +
        </button>
      </div>
    )}
  </Component>,
  DOM_NODE
);
```

Orrr you want to write some functions

```jsx
import Component from '@jbarzegar/Component'

<Component
  initialState={{
    count: 0
  }}
  handlers={{
    increment: ({ setState, state }, ...params) =>
      setState({ count: state.count + 1 }),
    decrement: ({ state, setState }, ...params) =>
      setState({ count: state.count - 1 })
  }}
>
  {({ handlers, state }) => (
    <div>
      <h1>{state.count}</h1>
      <button onClick={handlers.decrement}>-</button>
      <button onClick={handlers.increment}>+</button>
    </div>
  )}
</Component>
```

## 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 })`
* `handlers({ state, setState, props, forceUpdate }, ...params)`

### Some notes on `handlers`

It must be an object

## Legal

Released under MIT license.

Copyright &copy; 2017-present Ryan Florence

---
_Source: https://npm.io/package/@jbarzegar/component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
