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

eslint-plugin-named-functions-in-hooks

v1.0.6

Published

An ESLint plugin to enforce named functions in React hooks like useMemo, useCallback, and useEffect.

Downloads

507

Readme

eslint-plugin-named-functions-in-hooks

This ESLint plugin enforces the use of named functions instead of anonymous functions in React hooks such as useEffect, useCallback, and useMemo. Using named functions improves readability, debugging, and reusability of code within React components.


Why Use This Plugin?

Benefits of Named Functions in Hooks:

  1. Improved Debugging:
    • Named functions appear in stack traces, making debugging easier.
  2. Code Reusability:
    • Reusable named functions prevent redundant logic and improve maintainability.
  3. Readability:
    • Makes the intent of the function clearer, especially in complex components.

Installation

Install the plugin via npm:

npm install eslint-plugin-named-functions-in-hooks --save-dev

Usage

To enable the plugin in your ESLint configuration, add it to the plugins array and define the rule under rules:

Example .eslintrc.json:

{
  "plugins": ["named-functions-in-hooks"],
  "rules": {
     "named-functions-in-hooks/named-functions-in-hooks": "error"
  }
}

Rule Details

Rule: named-functions-in-hooks

This rule ensures that only named functions are passed to React hooks (useEffect, useCallback, useMemo).

Default Behavior

  • Disallows anonymous arrow functions and function expressions in the listed hooks.
  • Allows named functions and functions imported from other modules.

Examples

Correct Code:

Using named functions improves readability and debugging:

Named Function in useEffect:

  function logEffect() {
    console.log("Effect executed");
  }

  useEffect(logEffect, []);

  // or 

  React.useEffect(logEffect, []);

Named Function in useCallback:

  function memoizedHandler() {
    console.log("Handler executed");
  }

  const handler = useCallback(memoizedHandler, []);

  // or

  const handler = React.useCallback(memoizedHandler, []);

Named Function in useMemo:

  function computeExpensiveValue() {
    return someHeavyCalculation();
  }

  const memoizedValue = useMemo(computeExpensiveValue, [dependencies]);

  // or

  const memoizedValue = React.useMemo(computeExpensiveValue, [dependencies]);

Incorrect Code:

These examples will trigger an ESLint error:

Anonymous Function in useEffect:

  useEffect(() => {
    console.log("Effect executed");
  }, []); // Error: Only named functions are allowed in useEffect.

  // or 

  React.useEffect(() => {
    console.log("Effect executed");
  }, []); // Error: Only named functions are allowed in useEffect.

Anonymous Function in useCallback:

  const handler = useCallback(() => {
    console.log("Handler executed");
  }, []); // Error: Only named functions are allowed in useCallback.

  // or 

  const handler = React.useCallback(() => {
    console.log("Handler executed");
  }, []); // Error: Only named functions are allowed in useCallback.

Anonymous Function in useMemo:

  const memoizedValue = useMemo(() => someHeavyCalculation(), [dependencies]); // Error: Only named functions are allowed in useMemo.

  // or

  const memoizedValue = React.useMemo(() => someHeavyCalculation(), [dependencies]); // Error: Only named functions are allowed in useMemo.

How It Works

The plugin defines a custom ESLint rule that:

  1. Targets specific React hooks (useEffect, useCallback, and useMemo).
  2. Checks the first argument of the hook to determine if it is an anonymous function.
  3. Reports an error if the argument is an anonymous function.

The rule works with both functional and class components.


Configuration Options

Currently, the rule does not support additional configuration. Future updates may add customization options (e.g., extending the list of hooks).


Contributing

We welcome contributions to improve the plugin! Here’s how you can get started:

  1. Fork the repository and clone it locally.

  2. Install dependencies:

    npm install
  3. Implement your changes in the appropriate file (e.g., add a new feature or fix a bug).

  4. Write tests to ensure your changes work as expected.

  5. Open a pull request for review.

## License

This project is licensed under the MIT License.

Feedback and Issues

If you encounter any issues or have suggestions for improvement, please open an issue in the GitHub repository.

We’d love to hear your feedback and ideas!