import React from 'react'
import { Link, graphql } from 'gatsby'
import { Wrapper, SectionTitle, Header, SectionSubTitle, SEO } from 'components'
import { Layout } from 'layouts'
import { Content } from 'layouts/components'
import Helmet from 'react-helmet'
import config from 'config/siteConfig'
import Data from 'models/Data'
import styled from 'styled-components'
import * as everything from './about.mdx'
import { FeaturedPosts } from 'components/FeaturedPost'
interface Props {
data: Data
}
const HomePage = (props: Props) => {
// console.log()
const { edges } = props.data.allMdx
return (
<Layout>
<Helmet title={`Blog | ${config.siteTitle}`} />
<SEO path="/" data={{ title: config.siteTitleAlt }} />
<Header>
<SectionTitle>
{config.siteTitle}
<Line />
<SectionSubTitle>{config.siteDescription}</SectionSubTitle>
</SectionTitle>
</Header>
<Wrapper>
<MDXContent>
</MDXContent>
</Wrapper>
</Layout>
)
}
// Gatsby needs this default export to work.
// eslint-disable-next-line import/no-default-export
export default HomePage
export const query = graphql`
query {
allMdx(
sort: { fields: [frontmatter___date, frontmatter___title], order: DESC }
limit: 10
) {
totalCount
edges {
node {
fields {
path
}
frontmatter {
title
date(formatString: "MMMM D, YYYY")
standardDate: date(formatString: "YYYY-MM-DD")
}
excerpt(pruneLength: 200)
timeToRead
}
}
}
}
`
const Line = styled.hr`
color: white;
width: 5rem;
margin: 0.5rem auto;
height: 3px;
`
const Title = styled.span`
margin-right: 0.5rem;
`
const DateTag = styled.time`
color: rgba(0, 0, 0, 0.5);
`
I am new to web dev and react, mdx, graphql, tsx, gatsby.
Here is my index.tsx
and I also have about.mdx file in the same directory which renders inside /about path.
What I want to do is render the same content from about.mdx to my index.tsx file.
But I’m not sure how and where to put it and import it to my index.tsx
2
Answers
You can create another GraphQL query to fetch the
about.mdx
content in yourindex.tsx
:Double-check the query in the GraphiQL playground (
localhost:8000/___graphql
) in order to know the exactfileAbsolutePath
or to find another suitable filter likerelativePath
, the query structure and response.Once you validate the query, since it’s aliased with
aboutContent: allMdx
, your about content will be inside:Import the MDX file into your page and render it as a React component: