Skip to content

Instantly share code, notes, and snippets.

@chocolat5
Created January 13, 2020 14:01
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 chocolat5/f9235751628d1f8e341b4e71e7e0844f to your computer and use it in GitHub Desktop.
Save chocolat5/f9235751628d1f8e341b4e71e7e0844f to your computer and use it in GitHub Desktop.
React Hookを使用してGraphQLでナビゲーションにカテゴリーリストを表示する
import React from "react";
import { Link, useStaticQuery, graphql } from "gatsby";
const Navigation = () => {
const data = useStaticQuery(graphql`
query NavQuery {
categories: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___category) {
fieldValue
totalCount
}
}
}
`);
const categoryList = data.categories.group.map(cat => (
<li key={cat.fieldValue}>
<Link to={'/categories/' + cat.fieldValue}>
<FiFile />
{cat.fieldValue}
</Link>
</li>
));
return (
<nav>
<ul>
{categoryList}
</ul>
</nav>
);
};
export default Navigation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment