[英]ReactJS API to get all children of a rendered component
I am looking for a way to get all children out of a React Component. 我正在寻找一种使所有孩子脱离React组件的方法。
Example: 例:
class MyComponent extends React.Component {
render() {
<div>
<h1>Welcome!</h1>
<ChildComponent />
</div>
}
}
const renderedComponent = <MyComponent />
React.Children(renderedComponent.props.children).forEach(() => {
// Some logic
});
Problem: renderedComponent.props.children is empty. 问题:renderedComponent.props.children为空。 Is there any way to get the children in a way like this ? 有什么办法可以使孩子们像这样吗?
My goal is to get all children recursively. 我的目标是递归所有孩子。
看看ReatDOMServer似乎您想通过renderToString
将组件呈现为字符串,然后使用类似jQuery.parseHTML的方法解析结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.