npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-cached-handler

v1.0.7

Published

A tiny library that passes parameters to your event handlers using arrow functions without impacting rendering performance of components

Downloads

5

Readme

react-cached-handler

react-cached-handler is a tiny library that passes parameters to your event handlers using arrow functions without impacting rendering performance of components

Features

  • Named handlers
  • Handle events by arrow functions
  • Access to the key, custom arguments and the original event
  • Component rendering performace
  • Custom context for handlers

Download

Npm version

Package Installation

$ npm install react-cached-handler

How to use it?

You define a handler by calling createHandler. The first parameter is the context of your handler function that is optional

handler = createHandler(this)`

Now you can set your arrow function in events. The first argument must be a key.

<TextField
                label="Title"
                onBlur={this.handler('title', (fieldName, event)=> {} )}
                defaultValue = "default value" />

Parameters of the arrow function :

  • fieldName : The first parameter of the arrow function is allways the key you have specified
  • event : The second parameter is the original event. So you can access the value by event.target.value

You can pass even more arguments but the event argument is always the last one :

onBlur={this.handler('title', 'a1', 'a2', (fieldName, p1, p2, event)=> { console.log(p1) } )}
Note : Internally it caches your arrow functions by the specified key, no need to be worried about rerendering!

Example 1

EditPost Component that handles named events via a default handler function

import createHandler from 'react-cached-handler';

class EditPost extends React.PureComponent {

    handler = createHandler(this, (fieldName, e) => { console.log(e.target.value) } );

    render() {
        return <div>
        
            <TextField
                label="Title"
                onBlur={this.handler('title')}
                defaultValue = "default value" />

            <TextField
                label="Author Name"
                onBlur={this.handler('authorName')}
                defaultValue = "default value" />

            <TextField
                label="Post Content"
                onBlur={this.handler('postContent')}
                defaultValue = "default value" />

            <TextField
                label="Post Content"
                onBlur={this.handler('postContent' )}
                defaultValue = "default value" />

        </div>
    }
}
  • You could also override the default handler function
            <TextField
                label="Post Content"
                onBlur={this.handler('postContent', (fieldName, e) => { console.log('from custom handler function')} )}
                defaultValue = "default value" />

Example 2

PostMenu Component that handles selecting tags and posts

import React from 'react';
import createHandler from 'react-cached-handler';

class PostMenu extends React.PureComponent {

    postSelectHandler = createHandler(this);
    tagSelectHandler = createHandler(this);

    render() {
        return <div>
            {
                this.props.posts.map(post =>
                    <Post key={post.id}
                        onClick={this.postSelectHandler(post.id, (postId) => { console.log(postId) })}
                    />)
            }

            {
                this.props.tags.map(tag =>
                    <Tag key={tag}
                        onClick={this.tagSelectHandler(tag, (tag) => { console.log(tag) })}
                    />)
            }
    

        </div>
    }
}

Example 3

You can also pass more objects as arguments

handlePostClick = (postId, title, author) => {
    console.log(author.name)
}

.
.

<Post key={post.id}
    onClick={this.handler(post.id, post.Title, post.Author, this.handlePostClick)}
/>

Example 4

The original event object is passed as last argument

handlePostClick = (postId, title, author, e) => {
    console.log(e.target)
}

.
.

<Post key={post.id}
    onClick={this.handler(post.id, post.Title, post.Author, this.handlePostClick)}
/>

Example 5

It's ok if you don't assign the key. It uses 'default' for the key

firstNameChangeHandler = createHandler();

.
.

<TextField onChange={this.firstNameChangeHandler(e => {
        console.log(e.target.value);
    })}
/>

Or

firstNameChangeHandler = createHandler(e => {
    console.log(e.target.value);
});

.
.

<TextField onChange={this.firstNameChangeHandler()}
/>