# staged-components

> Make React function component staged.

Latest version **1.1.3** (published 2022-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install staged-components
pnpm add staged-components
yarn add staged-components
bun add staged-components
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2022-05-27 |
| First published | 2019-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | awmleer |
| Maintainers | awmleer |
| Keywords | react |

## Links

- npm: https://www.npmjs.com/package/staged-components
- Repository: https://github.com/awmleer/staged-components
- Homepage: https://github.com/awmleer/staged-components#readme
- Issues: https://github.com/awmleer/staged-components/issues
- npm.io page: https://npm.io/package/staged-components

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

- 1.1.3 (latest) — 2022-05-27
- 1.1.2 — 2021-10-27
- 1.1.1 — 2020-11-24
- 1.1.0 — 2020-11-17
- 1.0.0 — 2019-11-06

## README

# Staged Components

Make React function component staged.

## Install

```bash
yarn add staged-components
# or
npm install --save staged-components
```

## Usages

React Hook is awesome, but it has some kind of rules. One of these rules is ["Only Call Hooks at the Top Level"](https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level).

So the component below will cause an error:

```jsx
const App = function(props) {
  if (props.user === undefined) return null
  const [name, setName] = useState(props.user.name)
  // React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
  return (
    <input value={name} onChange={(e) => {setName(e.target.value)}}/>
  )
}
```

With `staged-components`, you can safely "break" this rule:

```jsx
const App = staged((props) => { // stage 1
  if (props.user === undefined) return null
  return () => { // stage 2
    const [name, setName] = useState(props.user.name)
    return (
      <input value={name} onChange={(e) => {setName(e.target.value)}}/>
    )
  }
})
```

## Advanced

Usage with `forwardRef`:

```jsx
const App = forwardRef(staged((props, ref) => {
  if (props.user === undefined) return null
  return () => {
    useImperativeHandle(ref, () => 'hello')
    return (
      <h1>{props.user.name}</h1>
    )
  }
}))
```

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