Giter VIP home page Giter VIP logo

react-use-event-hook's Introduction

react-use-event-hook

Same as React's useCallback, but returns a stable reference.

This library is a user-land implementation of the useEvent hook, proposed in this RFC.

Installation

npm install react-use-event-hook

Usage

(this example was copied from the RFC)

You can wrap any event handler into useEvent.

import useEvent from 'react-use-event-hook';

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

  const onClick = useEvent(() => {
    sendMessage(text);
  });

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

The code inside useEvent β€œsees” the props/state values at the time of the call. The returned function has a stable identity even if the props/state it references change. There is no dependency array.

See more

react-use-event-hook's People

Contributors

lynxtaa avatar scottrippey avatar shkreios avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-use-event-hook's Issues

Unexpected token "??"

I suggest replacing the operator "??" with "||", or lowering target in tsconfig, otherwise, because of one package, you have to install "babel/plugin-proposal-nullish-coalescing-operator"

~disregard~

router: provide a way to conditionally prevent navigation

wrong repo, ignore

Replacing useBrowserEffect with setting latestRef inline

Foremost, thank you for this nice library.

I am curious if the useBrowserEffect for setting the latestRef is necessary and if it can't be replaced with simply calling latestRef.current = callback inline i.e. replacing

useBrowserEffect(() => {
    latestRef.current = callback;
  }, [callback]);

with

latestRef.current = callback;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.