Last active
August 17, 2018 20:02
-
-
Save MaximeHeckel/592ba8c77332e09566d9e01ee79e2db0 to your computer and use it in GitHub Desktop.
Medium - React "sub-component" - Title sub-component
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
import React, { Component } from 'react'; | |
import findByType from './findByType'; | |
import css from './somestyle.css'; | |
// We instantiate the Title sub-component | |
const Title = () => null; | |
class Article extends Component { | |
// This is the function that will take care of rendering our Title sub-component | |
renderTitle() { | |
const { children } = this.props; | |
// First we try to find the Title sub-component among the children of Article | |
const title = findByType(children, Title); | |
// If we don’t find any we return null | |
if (!title) { | |
return null; | |
} | |
// Else we return the children of the Title sub-component as wanted | |
return <div className={css.title}>{title.props.children}</div>; | |
} | |
render() { | |
return ( | |
<div className={css.mainContainer}> | |
<div className={css.wrapper}> | |
<div className={css.titleContainer}> | |
{this.renderTitle()} | |
</div> | |
</div> | |
</div> | |
); | |
} | |
} | |
// Lastly we expose the Title sub-component through Article | |
Article.Title = Title; | |
export default Article; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment