Last active
June 1, 2017 00:45
-
-
Save Jerph/06eec4b35b2ead81af990b4a63cb2a42 to your computer and use it in GitHub Desktop.
Using child components props harmful?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DocumentTab extends React.Component { | |
static propTypes = Tab.propTypes; | |
static defaultProps = Object.assign({}, Tab.defaultProps, { | |
title: "Document Builds", | |
}); | |
render() { | |
const { document_builds, ...props } = this.props; | |
return ( | |
<Tab {...props}> | |
{(document_builds || []).map(document_build => ( | |
<DocumentBuild key="..." | |
document_build={document_build} | |
/> | |
))} | |
</Tab> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Tabs> | |
<Tab title="Normal Tab"> | |
<div>Some Content</div> | |
</Tab> | |
<DocumentTab document_builds={document_builds} /> | |
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not exported - Shows tab links | |
const TabNav = (props) => { | |
//... | |
return ( | |
<ul className={tabNavClassName} role='tablist'> | |
{ | |
React.Children.map(nonNullTabs, tab => ( | |
<TabItem | |
{...tab.props} // <-- This is the problem - when using DocumentTab, tab.props has no title | |
/> | |
)) | |
} | |
</ul> | |
); | |
}; | |
export const Tab = (props) => { | |
const { className, eventKey, activeKey, children } = props; | |
return ( | |
<TabContent className={className} active={eventKey === activeKey}> | |
{ children } | |
</TabContent> | |
); | |
}; | |
Tab.propTypes = { | |
title: PropTypes.string, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TabItem passing tab.props is the problem. My wrapper doesn't get title passed as a prop, and the defaultProp doesn't seem to be passed when props is accessed from the outside.