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.

React.cloneElement cannot remove existing props

See original GitHub issue

Do you want to request a feature or report a bug?

A feature.

What is the current behavior?

When an element is cloned with React.cloneElement, it’s possible to add new props or modify existing ones, but not to remove existing props.

Relevant code.

Example of how it works right now:

const element = React.createElement("a", {href: "http://github.com"});
const newElement = React.cloneElement(element, {href: undefined});
console.log(newElement.props); // {href: undefined}

What is the desired behavior?

It would be great to add some way to remove props (passing undefined as value?):

const element = React.createElement("a", {href: "http://github.com"});
const newElement = React.cloneElement(element, {href: undefined});
console.log(newElement.props); // {}

I guess I could use directly React.createElement but, AFAIK, I’ll have also to worry about special attributes like key and ref. I’d rather not mess with internals.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

I think it has worked this way in all React versions.

What’s your use case?

I am applying a map transformation of elements recursively and I need to remove some virtual props before passing the real elements for React to render. Console shows React does not recognize the [unknownProp] prop on a DOM element for those props, I’d want to avoid that.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
joaopedrodcfcommented, Sep 16, 2019

Any new update on this issue? 👍

5reactions
sophiebitscommented, Jan 28, 2019

We try to treat undefined the same as absent basically everywhere in React. Perhaps we should change the code that warns React does not recognize the unknownProp prop on a DOM element instead so that it is fine to leave an undefined in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove a prop from cloned React component
cloneElement doesn't let remove prop - they are copied and overwritten. No option for deleting or passing function. But looking a bit higher...
Read more >
Unknown Prop Warning
The unknown-prop warning will fire if you attempt to render a DOM element with a prop that is not recognized by React as...
Read more >
[Solved]-React - Remove prop from child-Reactjs
cloneElement argument to add new props and remove old props. Unneeded props should be assigned with undefined . You need to do this...
Read more >
useMenu – React Aria - React Spectrum Libraries
Documentation for useMenu in the React Aria package. ... cloneElement to apply the props, but you may want to use a more robust...
Read more >
React v15.0 – React Blog
We believe React will work in its current form there but we will not be ... If some of the props received by...
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