# react-callback-wrapper

> Allows to optimize components re-rendering using `memo`, substitutes `useCallback`.

Latest version **1.0.6** (published 2023-04-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-callback-wrapper
pnpm add react-callback-wrapper
yarn add react-callback-wrapper
bun add react-callback-wrapper
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2023-04-13 |
| First published | 2021-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | desofto |
| Keywords | react, callback, immutable |

## Links

- npm: https://www.npmjs.com/package/react-callback-wrapper
- Repository: https://github.com/desofto/react-callback-wrapper
- Issues: https://github.com/desofto/react-callback-wrapper/issues
- npm.io page: https://npm.io/package/react-callback-wrapper

## 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.6 (latest) — 2023-04-13
- 1.0.5 — 2023-04-12
- 1.0.4 — 2021-11-09
- 1.0.3 — 2021-11-09
- 1.0.2 — 2021-11-04
- 1.0.1 — 2021-11-02

## README

# react-callback-wrapper

Allows to optimize components re-rendering using `memo`, substitutes `useCallback`.

With `useCallback` we have a problem that when state is changed, callback function is re-generated as well (we should specify state as dependents) and, as result, all components which depend on this callback, are re-rendered.
Using `callback-wrapper` it is possible to keep callback function immutable and still have a fresh state inside.

Just install the package:

```
npm i --save react-callback-wrapper
```

And wrap your callback with `cbw`:

```
import React, { memo, useState } from 'react'
import ReactDOM from 'react-dom'
import cbw from 'react-callback-wrapper'

const Button = memo(({ onClick, children }) => {
  console.log('render')
  return (
    <button onClick={onClick}>{children}</button>
  )
})

function App() {
  const [count, setCount] = useState(0)

  const inc = cbw(() => {
    setCount(count + 1)
  })

  useEffect(() => {
    const timer = setInterval(inc, 1000)
    return () => clearInterval(timer)
  }, [])

  return (
    <>
      {count}
      <Button onClick={cbw(() => setCount(count + 1))}>inc</Button>
      <Button onClick={cbw(() => setCount(count - 1))}>dec</Button>
    </>
  )
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
)
```

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