1.1.3 • Published 2 years ago

use-memo-one v1.1.3

Weekly downloads
864,258
License
MIT
Repository
github
Last release
2 years ago

useMemoOne

useMemo and useCallback with a stable cache (semantic guarantee)

Build Status npm dependencies min minzip

Background

useMemo and useCallback cache the most recent result. However, this cache can be destroyed by React when it wants to:

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance. - React docs

useMemoOne and useCallbackOne are concurrent mode safe alternatives to useMemo and useCallback that do provide semantic guarantee. What this means is that you will always get the same reference for a memoized value so long as there is no input change.

Using useMemoOne and useCallbackOne will consume more memory than useMemo and useCallback in order to provide a stable cache. React can release the cache of useMemo and useCallback, but useMemoOne will not release the cache until it is garbage collected.

Install

# npm
npm install use-memo-one --save
# yarn
yarn add use-memo-one

Usage

import { useMemoOne, useCallbackOne } from 'use-memo-one';

function App(props) {
  const { name, age } = props;
  const value = useMemoOne(() => ({ hello: name }), [name]);
  const getAge = useCallbackOne(() => age, [age]);

  // ...
}

Aliased imports

You can use this import style drop in replacement for useMemo and useCallback

This style also plays very well with eslint-plugin-react-hooks.

import { useMemo, useCallback } from 'use-memo-one';

⚠️ The aliased exports useMemo and useCallback will only work if you use only use-memo-one and will clash if you also use useMemo or useCallback from react

import { useMemo, useCallback } from 'react';
// ❌ naming clash
import { useMemo, useCallback } from 'use-memo-one';

API

See useMemo and useCallback

Linting

useMemo and useCallback have fantastic linting rules with auto fixing in the eslint-plugin-react-hooks package. In order to take advantage of these with useMemoOne and useCallbackOne, structure your import like this:

import { useMemo, useCallback } from 'use-memo-one';
// Or your can alias it yourself
import {
  useMemoOne as useMemo,
  useCallbackOne as useCallback,
} from 'use-memo-one';

function App() {
  const [isActive] = useState(false);

  const onClick = useCallback(() => {
    console.log('isActive', isActive);

    // the input array will now be correctly checked by eslint-plugin-react-hooks
  }, [isActive]);
}

eslint rules

Here are some eslint rules you are welcome to use

module.exports = {
  rules: {
    // ...other rules

    'no-restricted-imports': [
      'error',
      {
        // If you want to force an application to always use useMemoOne
        paths: [
          {
            name: 'react',
            importNames: ['useMemo', 'useCallback'],
            message:
              '`useMemo` and `useCallback` are subject to cache busting. Please use `useMemoOne`',
          },
          // If you want to force use of the aliased imports from useMemoOne
          {
            name: 'use-memo-one',
            importNames: ['useMemoOne', 'useCallbackOne'],
            message:
              'use-memo-one exports `useMemo` and `useCallback` which work nicer with `eslint-plugin-react-hooks`',
          },
        ],
      },
    ],
  },
};
datavizlibreact-beautiful-dnd-ledger@infinitebrahmanuniverse/nolb-use-m@ahmedrowaihi/react-beautiful-dndreact-native-gifted-chat-18@everything-registry/sub-chunk-3027syntx-chatwanarn-storiessamf-custom-react-beautiful-dndreanimated-modalreact-wv-navigationrn-kore-bot-sdkrn-kore-bot-sdk-69rn-kore-bot-sdk-devreact-native-kora-botsdk@mytch/react-bdndmobx-easy-formlfbuilder-uiledger-react-beautiful-dndhaba-chat-uijotai-query-toolkitnodjs-mobile-native-templateparabaik-uipatched-peasyreact-dnd-beautifulreact-native-easy-chatreact-native-chiklotreact-pretty-dndreact-radarnusendra-react-native-gifted-chatreact-beautiful-dnd-grid-supportreact-beautiful-dnd-mobile-patchreact-beautiful-dnd-mouse-onlyreact-beautiful-dnd-nextreact-beautiful-dnd-short-pressreact-beautiful-dnd-suivireact-beautiful-dnd-temp-forkreact-beautiful-dnd-used-for-cabalareact-beautiful-dnd-used-private-1react-beautiful-dnd-with-custom-payloadreact-beautiful-dndreact-beautiful-dnd-chuck-wu-privatereact-beautiful-dnd-chuck-wu-private-1react-native-kore-botsdkreact-native-kore-botsdk-devreact-native-kore-botsdk-qareact-native-kore-botsdk-testreact-native-kore-bot-69-devreact-native-kore-bot-template-devreact-native-gifted-chat-changwoolabreact-native-gifted-chat-do-not-usereact-native-gifted-chat-exporeact-native-gifted-chat-forkreact-native-gifted-chat-forkedreact-native-modalfyreact-native-modalfy-ts-fixreact-native-gifted-chat-modern-lovereact-native-gifted-chat2react-native-gifted-chatssuivi-beautiful-dnd-with-custom-payloadtmt-react-native-gifted-chattreelab-dnd@armennalbandyan22/react-beautiful-dnd@atlaskit/legacy-mobile-macros@atlaskit/analytics-next@ahmedfouad2255/react-native-gifted-chat@aexer/react-native-gifted-chat@akojic27/react-beautiful-dnd@blocks/react-beautiful-dnd@beyond-labs/react-beautiful-dnd@brightalley/react-beautiful-dnd@buigabor/dnd@breeffy/react-native-calendar@breeze2/react-beautiful-dnd@byron-react-native/modalfy@11grossmane/react-beautiful-dnd-no-scroll@2060.io/react-native-gifted-chat@abenfield/react-beautiful-dnd@denis-sokolov/reactvana@fictivekin/react-beautiful-dnd@gsid/dnd@hello-pangea/dnd@happy-doc/dnd@iamlemon/rbd@hoory/react-beautiful-dnd@hufort/react-beautiful-dnd@gechiui/compose@gechiui/data@golocalinteractive/storage-essentials-components@gethigher/react-beautiful-dnd@krishna2323/react-beautiful-dnd@krotovic/dnd@measured/dnd@nebulae/react-beautiful-dnd@ninternal/react-beautiful-dnd@ndl_studio/react-native-gifted-chat@engineerforce/react-beautiful-dnd@erom/react-beautiful-dnd
1.1.3

2 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

6 years ago

1.1.0-beta.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago