Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arthurgeron/eslint-plugin-react-usememo

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arthurgeron/eslint-plugin-react-usememo

This plugin enforces the wrapping of complex objects or functions (which might generate unnecessary renders or side-effects) in `useMemo` or `useCallback`. It also allows you to programmatically enforce the wrapping of functional components in `memo`, and

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-7.98%
Maintainers
1
Weekly downloads
 
Created
Source

ESLint-Plugin-React-UseMemo

This plugin enforces the wrapping of complex objects or functions (which might generate unnecessary renders or side-effects) in useMemo or useCallback. It also allows you to programmatically enforce the wrapping of functional components in memo, and that all props and dependencies are wrapped in useMemo/useCallback.

Purpose

The objective is to ensure that your application's component tree and/or expensive lifecycles (such as React Native's FlatLists, useEffect, useMemo, etc.) only re-calculate or render again when absolutely necessary. By controlling expensive expressions, you can achieve optimal scalability and performance for your application.

Note: Use of memoization everywhere is not advised, as everything comes with a cost. Overusing memoization might slow down your application instead of speeding it up.

Guidelines for Memoization

Here are two primary rules for situations where dynamic objects should be memoed:

  1. Variables or expressions that return non-primitive objects or functions passed as props to other components.
  2. Variables or expressions that return non-primitive objects returned from custom hooks.

It is not recommended to use memoization in the following cases:

  • When the resulting value (expression or variable) is primitive (string, number, boolean).
  • If you're passing props to a native component of the framework (e.g. Div, Touchable, etc), except in some instances in react-native (e.g. FlatList).
  • Values that can be a global/context outside the react Context.

Example for better understanding:

Incorrect

function Component() {
  const breakpoints = [100];

  return <Modal breakpoints={breakpoints}>
}

Correct

const breakpoints = [100];
function Component() {
  return <Modal breakpoints={breakpoints}>
}

For more details, please refer to React Native's documentation on the importance of using static or memoized props for complex children.

Installation

Install it with yarn:

yarn add @arthurgeron/eslint-plugin-react-usememo --dev

or npm:

npm install @arthurgeron/eslint-plugin-react-usememo --save-dev

Usage

Add the plugin to your eslintrc file:

"plugins": ["@arthurgeron/react-usememo"],

Then enable any rules as you like:

"rules": {
    "@arthurgeron/react-usememo/require-usememo": [2],
},

In this guide, we will cover three rules - require-usememo, require-memo, and require-usememo-children.

This rule requires complex values (objects, arrays, functions, and JSX) that get passed props or referenced as a hook dependency to be wrapped in useMemo() or useCallback().

One of the great features of this rule is its amazing autofix functionality. It intelligently wraps necessary components with useMemo() or useCallback(), making your code more efficient and saving you valuable time.

For detailed examples, options available for this rule, and information about the autofix functionality, please refer to our rules documentation.

Rule #2: require-memo

This rule requires all function components to be wrapped in React.memo().

For detailed examples and usage of this rule, please refer to our rules documentation

Rule #3: require-usememo-children

This rule requires complex values (objects, arrays, functions, and JSX) that get passed as children to be wrapped in useMemo() or useCallback().

For detailed examples and options available for this rule, please refer to our rules documentation.

Conclusion

By efficiently using useMemo, useCallback, and React.memo(), we can optimize our React and React Native applications. It allows us to control the re-calculation and re-rendering of components, offering better scalability and performance.

FAQs

Package last updated on 19 Sep 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc