Skip to content

Instantly share code, notes, and snippets.

@wallawe
Created May 16, 2019 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallawe/214a7359af4607d2cc7aa8337bef96ce to your computer and use it in GitHub Desktop.
Save wallawe/214a7359af4607d2cc7aa8337bef96ce to your computer and use it in GitHub Desktop.
Best practices breadcrumb component for React following Google microdata guidelines
// Takes children and wraps them with microdata for SEO purposes
// https://developers.google.com/search/docs/data-types/breadcrumb#guidelines
const BreadCrumbs = ({children}) => (
<ol className="breadcrumbs" itemScope itemType="https://schema.org/BreadcrumbList">
{
React.Children.map(children, (child, i) => (
<li itemProp="itemListElement" itemScope itemType="https://schema.org/ListItem">
{
React.cloneElement(child, {
itemProp: 'item',
itemType: 'https://schema.org/Thing'
})
}
<meta itemProp="position" content={i + 1} />
</li>
))
}
</ol>
)
BreadCrumbs.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired
}
export default BreadCrumbs
<BreadCrumbs>
<a href="#">One</a>
<Link to="/">Two</Link>
</BreadCrumbs>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment