1.0.73 • Published 3 years ago

@fishbot/context-selector v1.0.73

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
3 years ago

Performance issue of React Context?

React Context and useContext is often used to avoid prop drilling, however it's known that there's a performance issue. When a context value is changed, all components calling useContext will re-render.

Example of React Context: https://codesandbox.io/s/context-selector-demo-ifnqr5?file=/src/ContextOriginal.js

// Result of the console
Render StoreProvider
Render ThemeConsumer
Render ThemeAction
Render LangConsumer
Render LangAction

When the context value is changed, all components re-render => performance issue

Ideally, in this example:

  • when changing theme => only StoreProvider and ThemeConsumer will re-render
  • when changing lang => only StoreProvider will re-render

What is ContextSelector?

ContextSelector can solve the problem above => performance increase.

Usage

// use these functions instead of the React `createContext` and `useContext`
import { createContextSelector, useContextSelector } from "@fishbot/context-selector";

Example of ContextSelector: https://codesandbox.io/s/context-selector-demo-ifnqr5?file=/src/ContextSelector.js

// Result of the console
Render StoreProvider
Render ThemeConsumer

When the context value is changed, only components using that value will re-render.

  • when changing theme => only StoreProvider and ThemeConsumer will re-render
  • when changing lang => only StoreProvider will re-render

Use case

State management with StoreContext: https://www.npmjs.com/package/@fishbot/store-context

More configs

Selector comparator

Currently, the default comparator used in the useContextSelector is deepEqual

(see https://gitlab.com/fishbot/libs/context-selector/-/blob/master/src/ContextSelector/utils/deepEqual.ts)

Basically, these are some comparison examples with deepEqual

  • Array comparison

    [] and [] => equal
    [1,2] and [1,2] => equal
    
    [] and [1] => not equal
    [1,2] and [1,3] => not equal
  • Object comparison

    {} and {} => equal
    {a: 1, b: 2} and {a: 1, b: 2} => equal
    
    {} and {a: 1} => not equal
    {a: 1, b: 2} and {a: 1, b: 3} => not equal

Note that, in vanilla JS

  • [] and [] are not equal, i.e. [] === [] is false
  • {} and {} are not equal, i.e. {} === {} is false
  • [1,2] and [1,2] are not equal, i.e. [1,2] === [1,2] is false
  • {a: 1, b: 2} and {a: 1, b: 2} are not equal, i.e. {a: 1, b: 2} === {a: 1, b: 2} is false

A custom comparator can be passed via the last parameter of the useContextSelector.

(see https://gitlab.com/fishbot/libs/context-selector/-/blob/master/src/ContextSelector/useContextSelector.ts)

Example:

const customComparator = (objA, objB) => {
  if (objA > objB) return true;
  return false;
}

const foo = useContextSelector(contextObj, contextValue => contextValue.foo, customComparator);
1.0.61

3 years ago

1.0.60

3 years ago

1.0.66

3 years ago

1.0.65

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.69

3 years ago

1.0.68

3 years ago

1.0.67

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.51

3 years ago

1.0.44

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago