简体   繁体   English

类型检查反应儿童

[英]Type Checking React Children

Question

How can I confirm that a react element received through props (like children for example) is of a given type in my render method? 如何确认通过道具(例如children )收到的反应元素在我的渲染方法中是否为给定类型?

Example: 例:

Say I have a List element and a ListItem element. 假设我有一个List元素和一个ListItem元素。 In the render method of List , I want to look for all of the children that were passed and do something special with any children that are ListItem s. List的render方法中,我想查找所有传递的子节点,并对ListItem的任何子ListItem执行特殊操作。

I did find an implementation that works, but only after trial and error. 我确实找到了一个有效的实现,但只有经过反复试验才能实现。 See the code below. 请参阅下面的代码。 (React 15.4.2) (React 15.4.2)

List.jsx List.jsx

import ListItem from 'list-item';

...

React.Children.map(children, child => {
  console.log(child);     // function ListItem() { ... }
  console.log(ListItem);  // function ListItem() { ... }

  if (isListItem(child)) {
    return (...);
  }
  return child;
})

// this implementation does not work
isListItem(element) {
  return (element.type === ListItem);
}

// this implementation does work, but might break if I'm using something like uglify or if I use `react-redux` and `connect()` ListItem (as that will change the display name)
isListItem(element) {
  return (element.type.displayName === 'ListItem');
}

// this implementation does work
isListItem(element) {
  return (element.type === (<ListItem />).type);
}

ListItem.jsx ListItem.jsx

class ListItem expends React.component {
  ...
}

export default ListItem;

So, the last implementation seems to work, but why doesn't the first implementation work? 所以,最后的实现似乎有效,但为什么第一个实现不起作用? I can't find any material relating to this in the react documentation, though I did find some stack overflow questions about the same thing. 我在反应文档中找不到与此相关的任何材料,尽管我确实发现了一些关于同一事情的堆栈溢出问题。 The answers provided in those questions, however, indicate that the first implementation should work (though they are for older versions of React) 但是,这些问题中提供的答案表明第一个实现应该有效(尽管它们适用于旧版本的React)

Relevant Links 相关链接

While this question is old, I ran into this problem while using react-hot-loader and took me a while to finally find this GitHub issue explaining why it behaved this way. 虽然这个问题很老,但我在使用react-hot-loader时遇到了这个问题并花了一些时间才终于发现这个GitHub问题,解释了为什么它表现得这样。

This is intended, react-hot-loader@3 patches React.createElement(<ImportedComponent />) is equivalent to React.createElement(ImportedComponent) so that it returns an element of a proxy wrapper for your components instead of the original component, this is part of allows to replace methods on the component without unmounting. 这是有意的,react-hot-loader @ 3补丁React.createElement(<ImportedComponent />)等效于React.createElement(ImportedComponent)因此它返回组件的代理包装器的元素而不是原始组件,这允许在不卸载的情况下替换组件上的方法。

- @nfcampos - @nfcampos

In addition to the methods you discovered, RHL now provides a areComponentsEqual() function with a dedicated section in their README . 除了您发现的方法之外,RHL现在还提供了一个areComponentsEqual()函数,其README中有一个专用部分

import { areComponentsEqual } from 'react-hot-loader'

const element = <Component />
areComponentsEqual(element.type, Component) // true

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

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