admin管理员组文章数量:1393350
I have a ponent that wraps other ponents:
class MyComp extends React.Component {
render() {
return <div> {this.props.children} </div>
}
}
Let's say I add another random ponent as a child:
<MyComp><FancyP>Hello</FancyP></MyComp>
The resultant HTML would be like this:
<div>
<p class="fancy">Hello</p>
</div>
I know that using React.Children
I could add new props to the child ponent, but what I really want to do is add custom attributes to the resultant HTML of any random child, having something like this:
<MyComp childAttr={{'data-x':'value'}}><FancyP>Hello</FancyP></MyComp>
that would generate the following:
<div>
<p class="fancy" data-x="value">Hello</p>
</div>
Is there a way to achieve this? Adding a props to the children does not work because children's classes are not aware of the new props and they ignore them.
I have a ponent that wraps other ponents:
class MyComp extends React.Component {
render() {
return <div> {this.props.children} </div>
}
}
Let's say I add another random ponent as a child:
<MyComp><FancyP>Hello</FancyP></MyComp>
The resultant HTML would be like this:
<div>
<p class="fancy">Hello</p>
</div>
I know that using React.Children
I could add new props to the child ponent, but what I really want to do is add custom attributes to the resultant HTML of any random child, having something like this:
<MyComp childAttr={{'data-x':'value'}}><FancyP>Hello</FancyP></MyComp>
that would generate the following:
<div>
<p class="fancy" data-x="value">Hello</p>
</div>
Is there a way to achieve this? Adding a props to the children does not work because children's classes are not aware of the new props and they ignore them.
Share Improve this question edited Sep 15, 2021 at 13:10 Lars Hendriks 1,0587 silver badges22 bronze badges asked Jun 22, 2016 at 10:13 Pablo LozanoPablo Lozano 10.4k2 gold badges43 silver badges63 bronze badges1 Answer
Reset to default 4Not sure why you need this and I do remend you to rethink your architecture before it is too late.
But you can do a little bit of a hackery using ReactDOM.findDOMNode
.
First you need to set refs for every child ponent. By cloning element and assigning a ref.
Then attach hooks on ponentDidMount
and ponentDidUpdate
events, find dom element using findDOMNode
and manually populate dataset. DEMO.
import React, { Component, Children, cloneElement } from 'react'
import { render, findDOMNode } from 'react-dom'
class MyComp extends Component {
ponentDidMount() {
this.setChildAttrs()
}
ponentDidUpdate() {
this.setChildAttr()
}
setChildAttrs() {
const { childAttrs } = this.props
const setAttrs = el => Object.keys(childAttrs)
.forEach(attr => el.dataset[attr] = childAttrs[attr])
// for each child ref find DOM node and set attrs
Object.keys(this.refs).forEach(ref => setAttrs(findDOMNode(this.refs[ref])))
}
render() {
const { children } = this.props
return (<div> {
Children.map(children, (child, idx) => {
const ref = `child${idx}`
return cloneElement(child, { ref });
})} </div>)
}
}
本文标签: javascriptReact Can I add attributes to children39s resultant HTMLStack Overflow
版权声明:本文标题:javascript - React: Can I add attributes to children's resultant HTML? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763383a2623886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论