admin管理员组文章数量:1202195
I'm trying to make my own Tabs
component, so that I can use tabs in my app. However I seem to be having issues trying to extract the child components I need by type.
import React from 'react'
export class Tabs extends React.Component {
render() {
let children = this.props.children
let tablinks = React.Children.map(children, x => {
console.log(x.type.displayName) // Always undefined
if (x.type.displayName == 'Tab Link') {
return x
}
})
return (
<div className="tabs"></div>
)
}
}
export class TabLink extends React.Component {
constructor(props) {
super(props)
this.displayName = 'Tab Link'
}
render() {
return (
<div className="tab-link"></div>
)
}
}
<Tabs>
<TabLink path="tab1">Test</TabLink>
<TabLink path="tab2">Test2</TabLink>
</Tabs>
My console.log
never returns "Tab Link", it always returns undefined
, why?
I'm trying to make my own Tabs
component, so that I can use tabs in my app. However I seem to be having issues trying to extract the child components I need by type.
import React from 'react'
export class Tabs extends React.Component {
render() {
let children = this.props.children
let tablinks = React.Children.map(children, x => {
console.log(x.type.displayName) // Always undefined
if (x.type.displayName == 'Tab Link') {
return x
}
})
return (
<div className="tabs"></div>
)
}
}
export class TabLink extends React.Component {
constructor(props) {
super(props)
this.displayName = 'Tab Link'
}
render() {
return (
<div className="tab-link"></div>
)
}
}
<Tabs>
<TabLink path="tab1">Test</TabLink>
<TabLink path="tab2">Test2</TabLink>
</Tabs>
My console.log
never returns "Tab Link", it always returns undefined
, why?
- Did you mean to have TabLink extend Tabs? Right now it's just a sibling as it extends React.Component. – Jecoms Commented Aug 29, 2016 at 18:39
- @Jecoms Wait, I have to extend Tabs? How would that work? Doesn't it need to extend React.Component to work? – Sebastian Olsen Commented Aug 29, 2016 at 18:40
- I misunderstood your intent. I feel like x.type is the problem, but I'd have to research further. – Jecoms Commented Aug 29, 2016 at 18:53
4 Answers
Reset to default 14As an alternative you could use
console.log(x.type.name) // this would be 'TabLink'
You don't need to explicitly define displayName in this case.
https://jsfiddle.net/lustoykov/u1twznw7/1/
It's undefined
because displayName
should be a static
property.
class TabLink extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="tab-link"></div>
)
}
}
TabLink.displayName = 'Tab Link'
jsfiddle (check the console)
You can use the already defined name
property:
if (x.type.name === TabLink.name) {
return x
}
I recommend to use TabLink.name
instead of 'TabLink'
string for better maintenance.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
I'm wondering why does your Tabs
component need to know how to render each children.
You could have a specific component for each type of tab, with their own styles and with 2 props: isSelected
and onSelect
.
Then the Tabs
would only:
- Render its children, inline, with the correct space among them ("display:flex" with column-gap)
- Control the tab selection, by keeping the state of the selected tab and handling the
onSelect
of each tab (to update the selectedTab state and to pass true in theisSelected
prop of the correct tab)
本文标签: javascriptHow to get children type in reactStack Overflow
版权声明:本文标题:javascript - How to get children type in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738584514a2101352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论