New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-callback-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-callback-wrapper

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

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source

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')
)

Keywords

react

FAQs

Package last updated on 13 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts