question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@babel/react: useSpread needlessly clones props

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior Enabling useSpread needlessly clones props:

React.createElement(SomeComponent, { ...props })

With useSpread disabled, you can see that the object is not cloned:

React.createElement(SomeComponent, props)

Input Code

  • REPL or Repo link if applicable:
function MyComponent(props) {
  return <SomeComponent {...props} />;
}

I can’t find a toggle for useSpread in the babel REPL. 🤷

Expected behavior/code When a component only has 1 spread object as prop, no other props/second spread, there is no need to clone the object.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.json
{
  "presets": [
    ["@babel/env", {
      "bugfixes": true,
      "corejs": 3,
      "useBuiltIns": "entry"
    }],
    "@babel/typescript",
    ["@babel/react", { "useSpread": true }]
  ],
  "plugins": [
    ["@babel/transform-runtime", { "useESModules": true }],
    "@babel/proposal-class-properties"
  ]
}

Environment

  • Babel version(s): [e.g. v6.0.0, v7.0.0-beta.34] ^7.9.4
  • Node/npm version: [e.g. Node 8/npm 5] Node 13.12.0/npm 6.14.4
  • OS: [e.g. OSX 10.13.4, Windows 10] Win10
  • Monorepo: [e.g. yes/no/Lerna] no
  • How you are using Babel: [e.g. cli, register, loader] loader

Possible Solution

Avoid extra clonings.

Additional context/Screenshots Add any other context about the problem here. If applicable, add screenshots to help explain. None.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
trueadmcommented, Apr 14, 2020

Cloning the props is an important heuristic here.

It means that the object that gets passed to React’s jsx is a plain mutable object with simple properties; otherwise if you passed in props directly there are no guarantees.

Some of the properties might not be enumerable, or the properties might be getters/setters, or the object might be frozen/sealed (meaning React would have to clone it again if it wanted to mutate the object). By cloning it, we get a snapshot that is final.

1reaction
nstepiencommented, Apr 14, 2020

@jridgewell I guess we can reopen and only address the createElement case then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-addons-clone-with-props | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
babel/plugin-proposal-object-rest-spread
NOTE**: This plugin is included in `@babel/preset-env`, ... spread defines new properties, while Object.assign() sets them, so using this mode might produce ...
Read more >
Complete Intro to React
A Complete Intro to React, as taught for FrontendMasters.com.
Read more >
30-days-of-react-ebook-fullstackio.pdf
underpinnings of how the framework works, but be able to use React in your ... Inside of our component, we can access this...
Read more >
react.pdf - SlideShare
Higher Order Component that checks for authentication 34 Chapter 7: How and why to use. Props Default Value 50 Spread Attributes 51 Children...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found