简体   繁体   English

从克隆的 React 组件中删除一个 prop

[英]Remove a prop from cloned React component

Goal: I need to perform some post-processing of React components, and that involves removing some props.目标:我需要对 React 组件进行一些后期处理,这涉及删除一些道具。 I tried to use React.cloneElement passing {propToRemove: undefined} as the second argument, but the prop is not removed, just set to undefined .我尝试使用React.cloneElement传递{propToRemove: undefined}作为第二个参数,但道具没有被删除,只是设置为undefined I could use React.createElement , but according to the docs, that would lose ref s, which is a serious drawback.我可以使用React.createElement ,但根据文档,这会丢失ref ,这是一个严重的缺点。

A contrived example not doing anything useful, just to test:一个人为的例子没有做任何有用的事情,只是为了测试:

const removeUnknownPropWithClone = (el) => {
  return React.cloneElement(el, {unknownProp: undefined})
};

const App = (props) => 
  removeUnknownPropWithClone(
    <div unknownProp="1">Hello</div>
  );

This shows the error message: "React does not recognize the unknownProp prop on a DOM element".这显示了错误消息:“React 无法识别 DOM 元素上的unknownProp道具”。 Indeed, the prop is set to undefined , but it's still there.实际上,道具设置为undefined ,但它仍然存在。 I need to completely remove it.我需要完全删除它。

Runnable snippet (open the console to see the error messages)Runnable 片段(打开控制台查看错误消息)

Related question (but not answered there): React - Remove prop from child相关问题(但没有在那里回答): React - Remove prop from child

Relevant source code: https://github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L293相关源码: https : //github.com/facebook/react/blob/master/packages/react/src/ReactElement.js#L293

Sometimes it's good to look at sources ;)有时查看来源很好;)

cloneElement doesn't let remove prop - they are copied and overwritten. cloneElement不允许删除道具 - 它们被复制和覆盖。 No option for deleting or passing function.没有删除或传递函数的选项。

But looking a bit higher we can see:但是再往上看,我们可以看到:

export function cloneAndReplaceKey(oldElement, newKey) {
  const newElement = ReactElement(
    oldElement.type,
    newKey,
    oldElement.ref,
    oldElement._self,
    oldElement._source,
    oldElement._owner,
    oldElement.props,
  );

  return newElement;
}

Looks easy to extend but ReactElement isn't exported :]看起来很容易扩展,但 ReactElement 没有导出 :]

... but it looks like ref , key and trimmed props can be copied/passed (by config) to createElement . ...但看起来refkey和修剪过的道具可以(通过配置)复制/传递到createElement Check if it will be sufficient.检查它是否足够。

Very simple with JSX (inside a Children.map() function):使用 JSX 非常简单(在Children.map()函数中):

const { filtered, ...rest } = child.props
const clone = <child.type key={child.key} ref={child.ref} {...rest} />

We solved it this way我们是这样解决的

const { propToRemove, ...childProps } = child.props
const filteredChild = { ...child, props: childProps }

return React.cloneElement(filteredChild, additionalProps)

You could copy the element props to another object and delete the undesired prop in the new object您可以将元素道具复制到另一个对象并删除新对象中不需要的道具

const removeUnknownPropWithClone = (el) => {
  const props = { ...el.props }
  delete props['unknownProps']
  return React.cloneElement(el, props)
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM