All Projects → DAB0mB → Babel Plugin React Persist

DAB0mB / Babel Plugin React Persist

Licence: mit
Automatically useCallback() & useMemo(); memoize inline functions

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin React Persist

Sowing Machine
🌱A React UI toolchain & JSX alternative
Stars: ✭ 64 (-29.67%)
Mutual labels:  babel, babel-plugin, jsx
Jsx Control Statements
Neater If and For for React JSX
Stars: ✭ 1,305 (+1334.07%)
Mutual labels:  babel, babel-plugin, jsx
Babel Plugin React Html Attrs
Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes
Stars: ✭ 170 (+86.81%)
Mutual labels:  babel, babel-plugin, jsx
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (-28.57%)
Mutual labels:  babel, ast, babel-plugin
Babel Plugin Jsx Adopt
Stars: ✭ 94 (+3.3%)
Mutual labels:  babel, babel-plugin, jsx
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (+327.47%)
Mutual labels:  babel, ast, babel-plugin
Htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Stars: ✭ 7,299 (+7920.88%)
Mutual labels:  babel, babel-plugin, jsx
Babel Plugin Css Prop
Babel plugin to transpile `css` prop to a styled component. (Experimental)
Stars: ✭ 56 (-38.46%)
Mutual labels:  babel, babel-plugin
Tinker.macro
Evaluate Laravel code at build-time, via Laravel Tinker
Stars: ✭ 56 (-38.46%)
Mutual labels:  babel, babel-plugin
Jsx Ast Utils
AST utility module for statically analyzing JSX
Stars: ✭ 89 (-2.2%)
Mutual labels:  ast, jsx
Xwasm
[Work In Progress] WebAssembly Packager and WASM tooling for modern frontend
Stars: ✭ 45 (-50.55%)
Mutual labels:  babel, babel-plugin
Babel Plugin Partial Application
[DEPRECATED] Please use https://github.com/citycide/param.macro
Stars: ✭ 60 (-34.07%)
Mutual labels:  babel, babel-plugin
Modify Babel Preset
💫 Create a modified babel preset based on an an existing preset.
Stars: ✭ 85 (-6.59%)
Mutual labels:  babel, babel-plugin
Babel Plugin Root Import
Add the opportunity to import modules by the root path
Stars: ✭ 1,084 (+1091.21%)
Mutual labels:  babel, babel-plugin
Js Toolbox
CLI tool to simplify the development of JavaScript apps/libraries with little to no configuration. (WORK IN PROGRESS/PACKAGE NOT PUBLISHED).
Stars: ✭ 53 (-41.76%)
Mutual labels:  babel, jsx
React Es6 Padawan To Jedi Book
Uma introdução simples e completa para React usando ES6 e Babel.
Stars: ✭ 46 (-49.45%)
Mutual labels:  babel, jsx
Tkframework
react + relay + redux + saga + graphql + webpack
Stars: ✭ 83 (-8.79%)
Mutual labels:  babel, jsx
Babel Plugin Optimize Clsx
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API
Stars: ✭ 80 (-12.09%)
Mutual labels:  babel, babel-plugin
Generator Babel Plugin
Babel Plugin generator for Yeoman
Stars: ✭ 88 (-3.3%)
Mutual labels:  babel, babel-plugin
Compiled
A familiar and performant compile time CSS-in-JS library for React.
Stars: ✭ 1,235 (+1257.14%)
Mutual labels:  babel, babel-plugin

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].