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

recallback

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recallback

A React hook for creating cached callback

latest
Source
npmnpm
Version
0.0.9
Version published
Maintainers
1
Created
Source

recallback

A React hook for creating cached callback.

Inspired by this RFC https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md

Installation

with NPM

npm i recallback --save

with YARN

yarn add recallback

Usages

Using single callback

import useCallback from "recallback";

function Chat() {
  const [text, setText] = useState("");

  const onClick = useCallback(
    () => {
      sendMessage(text);
    } /* no deps needed */
  );

  return <SendButton onClick={onClick} />;
}

Using callback factory

Let say you have a todo list, and you want to handle toggle/remove action for each todo item, please check example below

import useCallback from "recallback";

function TodoList({ todos }) {
  const handleToggleOf = useCallback((todoId) =>
    // return a click callback for each todo item
    // recallback will cache all callbacks
    () => {
      // do something
    }
  );

  return todos.map((todo) => (
    <TodoItem key={todo.id} id={todo.id} onToggle={handleToggleOf(todo.id)} />
  ));
}

Using key selector for callback factory

By default, recallback uses first argument as callback, if the first argument is complex object, you can pass key selector to indicate which value is a key

import useCallback from "recallback";

function TodoList({ todos }) {
  const handleToggleOf = useCallback(
    (todo) => () => {},
    // select todo id from todo object
    (todo) => todo.id
  );

  return todos.map((todo) => (
    <TodoItem key={todo.id} id={todo.id} onToggle={handleToggleOf(todo)} />
  ));
}

Optimizing for children function

import { memo, ReactNode, useEffect, useState } from "react";
import useCallback from "recallback";
import "./styles.css";

const LoadContent = memo((props) => {
  const [loading, setLoading] = useState(true);
  console.log("re-render");
  useEffect(() => {
    setTimeout(setLoading, 10000, false);
  }, [setLoading]);

  // call children
  return <div>{props.children(loading)}</div>;
});

export default function App() {
  const [counter, setCounter] = useState(0);
  const renderContent = useCallback((loading) => {
    return <span>{loading && "Loading..."}</span>;
  });

  return (
    <div className="App">
      <button onClick={() => setCounter(counter + 1)}>
        Counter: {counter}
      </button>
      <LoadContent>{renderContent}</LoadContent>
    </div>
  );
}

Keywords

react

FAQs

Package last updated on 21 May 2022

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