Skip to content

Instantly share code, notes, and snippets.

@Jerph
Last active June 1, 2017 00:45
Show Gist options
  • Save Jerph/06eec4b35b2ead81af990b4a63cb2a42 to your computer and use it in GitHub Desktop.
Save Jerph/06eec4b35b2ead81af990b4a63cb2a42 to your computer and use it in GitHub Desktop.
Using child components props harmful?
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>
);
}
}
<Tabs>
<Tab title="Normal Tab">
<div>Some Content</div>
</Tab>
<DocumentTab document_builds={document_builds} />
</Tabs>
// 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,
};
@Jerph
Copy link
Author

Jerph commented May 31, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment