# stateful-hooks

> Give your react hooks state on the server side

Latest version **1.0.1** (published 2020-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install stateful-hooks
pnpm add stateful-hooks
yarn add stateful-hooks
bun add stateful-hooks
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-01-31 |
| First published | 2020-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | @davidmarkclem |
| Maintainers | davidmarkclements |
| Keywords | react, hooks, server side rendering |

## Links

- npm: https://www.npmjs.com/package/stateful-hooks
- Repository: https://github.com/davidmarkclements/stateful-hooks
- Homepage: https://github.com/davidmarkclements/stateful-hooks#readme
- Issues: https://github.com/davidmarkclements/stateful-hooks/issues
- npm.io page: https://npm.io/package/stateful-hooks

## 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.0.1 (latest) — 2020-01-31

## README

<h1 align="center">stateful-hooks</h1>
<p>
  <img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
  <a href="https://github.com/davidmarkclements/stateful-hooks#readme" target="_blank">
    <img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
  </a>
  <a href="https://github.com/davidmarkclements/stateful-hooks/graphs/commit-activity" target="_blank">
    <img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" />
  </a>
  <a href="https://github.com/davidmarkclements/stateful-hooks/blob/master/LICENSE" target="_blank">
    <img alt="License: MIT" src="https://img.shields.io/github/license/davidmarkclements/stateful-hooks" />
  </a>
  <a href="https://twitter.com/davidmarkclem" target="_blank">
    <img alt="Twitter: davidmarkclem" src="https://img.shields.io/twitter/follow/davidmarkclem.svg?style=social" />
  </a>
</p>

> Give your react hooks state on the server side

### 🏠 [Homepage](https://github.com/davidmarkclements/stateful-hooks#readme)

## Usage

This is a drop-in module that will modifiy React hooks SSR behaviour.

Use as a preloaoder

```shh
node -r stateful-hooks my-app.js
```

Or react in the entry point before requiring react:

```js
require('stateful-hooks')
const React = require('react')
```

## Install

```sh
npm install
```

## Run tests

```sh
npm test
```

## Stateful Hooks

The following hooks retain state on the server side when `stateful-hooks` has been applied:

* useState - persists state across requests
* useReducer  - persists state across requests 
* useMemo - persists memoization across requests
* useCallback - persists memoization across requests
* useContext - supplies context state during ssr
* useRef - returns `{current: input}` instead of `undefined`
* useEffect - can be enabled on a case by case basis by setting `useEffect.ssr` to true. In SSR mode use effect is called during rendering.

## Server-side `useEffect`

`useEffect` is a client-side only hook, however we can opt in to making the useEffect call into a server-side hook by setting `useEffect.ssr = true` right before it is called:

```js
import { useEffect } from 'react'
const MyComponent = () => {
  const [ state, update ] = useState(0)
  useEffect.ssr = true
  useEffect(() => {
    update(1) // this will be called server side
  })
  useEffect(() => {
    update(2) // this will NOT be called server side
  })
}
```

## Avoiding server state

In the event that server state is unwanted in a particular context, ensure the `update` function is called within `useEffect` (without setting `useEffect.ssr` to true) so that it's client side only. 

## Author

👤 **David Mark Clements**

* Twitter: [@davidmarkclem](https://twitter.com/davidmarkclem)
* Github: [@davidmarkclements](https://github.com/davidmarkclements)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/davidmarkclements/stateful-hooks/issues). 

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Copyright © 2020 [David Mark Clements](https://github.com/davidmarkclements).<br />
This project is [MIT](https://github.com/davidmarkclements/stateful-hooks/blob/master/LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

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