• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically useCallback() & useMemo(); memoize inline functions

CircleCI

babel-plugin-react-persist

A Babel plug-in that optimizes your React.Component's implementation by automatically detecting declarations that should persist between rendering phases and replacing them with useCallback() and useMemo() whenever necessary. This plug-in can also be used to solve excessive processing power caused by using anonymous callbacks provided to JSX element by making them non-anonymous. Note that this plug-in is experimental and shouldn't be used in production yet. Compatible with React 16.8-alpha and above (hooks support).

Example

in

export default ({ data, sortComparator, filterPredicate, history }) => {
  const transformedData = data.filter(filterPredicate).sort(sortComparator)

  return (
    <div>
      <button className="back-btn" onClick={() => history.pop()} />
      <ul className="data-list">
        {transformedData.map(({ id, value }) => (
          <li className="data-item" key={id} onClick={() => history.push(`data/${id}`)}>{value}</li>
        ))}
      </ul>
    </div>
  )
}

out

let _anonymousFnComponent, _anonymousFnComponent2

export default ({ data, sortComparator, filterPredicate, history }) => {
  const transformedData = React.useMemo(() =>
    data.filter(filterPredicate).sort(sortComparator)
  , [data, data.filter, filterPredicate, sortComparator])

  return React.createElement(_anonymousFnComponent2 = _anonymousFnComponent2 || (() => {
    const _onClick2 = React.useCallback(() => history.pop(), [history, history.pop])

    return (
      <div>
        <button className="back-btn" onClick={_onClick2} />
        <ul className="data-list">
          {transformedData.map(({ id, value }) =>
            React.createElement(_anonymousFnComponent = _anonymousFnComponent || (() => {
              const _onClick = React.useCallback(() =>
                history.push(`data/${id}`)
              , [history, history.push, id])

              return (
                <li className="data-item" key={id} onClick={_onClick}>
                  {value}
                </li>
              )
            }), { key: id })
          )}
        </ul>
      </div>
    )
  }), null)
}

Usage

babel-plugin-react-persist is installable via NPM (or Yarn):

$ npm install babel-plugin-react-persist

Add to .babelrc under plugins and be sure to load it before any JSX transformation related plug-ins.

{
  "presets": ["@babel/preset-react"],
  "plugins": ["babel-plugin-react-persist"]
}

License

MIT. If you're including this in a repo above 1k stars I would really appreciate it if you could contact me first.

More Repositories

1

Appfairy

A CLI tool to Migrate a Webflow project into a React app
JavaScript
283
star
2

ReactNativeMeteorBoilerplate

JavaScript
78
star
3

radial-snake

A tutorial for creating a Tron-style game
JavaScript
73
star
4

node-distance-addon

Native NodeJS add-on creation tutorial using C++
C++
69
star
5

jsx-runtime

JavaScript
38
star
6

angular-ecmascript

Build an AngularJS app using ES6's class system
JavaScript
28
star
7

graphql-tag-pluck

Pluck graphql-tag template literals
JavaScript
25
star
8

react-fs-tree

JavaScript
25
star
9

eventsheet-wasm

An extensible custom CSS engine that uses WASM
JavaScript
25
star
10

HotnCold

JavaScript
23
star
11

babel-plugin-scoped-styled-components

JavaScript
13
star
12

usertron

TypeScript
12
star
13

react-autorun

A macro that compiles into a dependencies array for hooks.
TypeScript
12
star
14

git-streamer

JavaScript
11
star
15

redis-node-server

TypeScript
10
star
16

HotnCold-server

JavaScript
10
star
17

react-layers-stack

Push and pop React view layers with transitions
JavaScript
7
star
18

cla6

ES6 style class system
JavaScript
5
star
19

babel-plugin-tester

JavaScript
5
star
20

react-bobcat

A library for testing navigation flows in React
JavaScript
4
star
21

parallax-chess

TypeScript
4
star
22

meteor-npm-ionic

Ionic scripts and assets packs
JavaScript
3
star
23

get-updates

A utility for getting mongo updates
JavaScript
2
star
24

02hero

A JavaScript Crash Course
2
star
25

cla6-hidden

Hidden plugin for Cla6.js
JavaScript
2
star
26

cla6-base

Base plugin for Cla6.js
JavaScript
1
star
27

angular-ionic

Ionic's ng-components pack
JavaScript
1
star
28

event-ops

Event Ops (Event Operations) is a library that provides very simple and minimalistic utils to create event-driven programs.
TypeScript
1
star
29

react-async-core-hooks

Async versions of core React hooks e.g. useAsyncEffect
JavaScript
1
star