React Markdown Examples
Sometimes you just want to use basic React app without back-end and add Markdown content to it instead of implementing all of the posting functionality. In that case you can use React Markdown component.
After playing around with it for a bit, I just wanted to show some examples of using React Markdown, so here they are.
I’m using Vite to create a React app and React Router to manage routes, plus we need to install React Markdown itself, so here are the initial commands:
npm create vite@latest
npm install react-router-dom react-markdownThe most basic example is to create a simple React component, add a variable with your Markdown-styled text and then use it in the component itself by wrapping it with <ReactMarkdown>.
import ReactMarkdown from "react-markdown";
const markdownText = `
# React Markdown Example
- Some text
- Some other text
## Subtitle
### Additional info
This is a [link](https://github.com/remarkjs/react-markdown)
`;
function MarkdownComponent() {
return (
<section>
<ReactMarkdown>{markdownText}</ReactMarkdown>
</section>
);
}
export default MarkdownComponent;You can also add syntax highlighting to your code blocks, but for that you need to install React Syntax Highlighter component:
npm i react-syntax-highlighterThen you have to modify your <ReactMarkdown> component in the following way (additional details can be checked in React Markdown readme; for the current example I’ve slightly adjusted it to avoid passing children as props):
import ReactMarkdown from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";
const markdownText = `
# React Markdown Example
~~~js
const aJsVariable = "Test";
console.log(aJsVariable);
~~~
`;
function MarkdownComponent() {
return (
<section>
<ReactMarkdown
components={{
code({ className, children, ...rest }) {
const match = /language-(\w+)/.exec(className || "");
return match ? (
<SyntaxHighlighter
PreTag="div"
language={match[1]}
style={dark}
{...rest}
>
{children}
</SyntaxHighlighter>
) : (
<code {...rest} className={className}>
{children}
</code>
);
},
}}
>
{markdownText}
</ReactMarkdown>
</section>
);
}
export default MarkdownComponent;Keep in mind that you have to adjust the code-part of your Markdown content that you want to highlight:
~~~js
const aJsVariable = "Test";
console.log(aJsVariable);
~~~
// Another possible variant:
\`\`\`javascript
const aJsVariable = "Test";
console.log(aJsVariable);
\`\`\`Next, we can fetch Markdown content from an external source (file or API). For that we need to use React hooks useState and useEffect. Here is an example with a fetch request to a file located in the public folder of the React app:
import { useEffect, useState } from "react";
import ReactMarkdown from "react-markdown";
function MarkdownComponent() {
const [markdownContent, setMarkdownContent] = useState("");
useEffect(() => {
fetch("/posts/post1.md")
.then((response) => response.text())
.then((text) => setMarkdownContent(text));
}, []);
return (
<section>
<ReactMarkdown>{markdownContent}</ReactMarkdown>
</section>
);
}
export default MarkdownComponent;In this example post1.md file has the same text as markdownText variable from the first example, so it will render the same page (the first screenshot above).
Get Dimterion’s stories in your inbox
Join Medium for free to get updates from this writer.
Probably the most interesting example for React Markdown is using it for rendering multiple pages based on the available .md files (for instance, several blog posts with each one having its own .md file, so you can put files with Markdown texts in your posts-folder and automatically render posts pages for them).
For that you’ll need to additionally use React Router (that’s why it was installed at the beginning).
We already have a folder with some .md files for our posts, so they’ll be accessed from there.
After that we need to set routes for our app. Here is a basic version with a Home page and separate routes for each post:
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import MarkdownPage from "./components/MarkdownPage";
import HomePage from "./components/HomePage";
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/:filename" element={<MarkdownPage />} />
</Routes>
</Router>
);
}
export default App;For simplicity, the Home page will just have an unordered list of links to our posts made with React Router <Link> elements:
import { Link } from "react-router-dom";
function HomePage() {
return (
<main>
<h1>Posts</h1>
<ul>
<li>
<Link to="/post1">Post 1</Link>
</li>
<li>
<Link to="/post2">Post 2</Link>
</li>
<li>
<Link to="/post3">Post 3</Link>
</li>
</ul>
</main>
);
}
export default HomePage;And finally we write a component to create the pages for each post. Additionally to useState and useEffect, we also need to add useParams hook from React Router to be able to access different files from our posts folder. Same as before, we make a fetch request (adjusting the path with the help of useParams) and generate the posts content:
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import ReactMarkdown from "react-markdown";
function MarkdownPage() {
const { filename } = useParams();
const [markdownContent, setMarkdownContent] = useState("");
useEffect(() => {
fetch(`/posts/${filename}.md`)
.then((response) => response.text())
.then((text) => setMarkdownContent(text));
}, [filename]);
return <ReactMarkdown>{markdownContent}</ReactMarkdown>;
}
export default MarkdownPage;This will give us a Home page that stores links for the individual posts pages.
These are of course basic examples of using React Markdown and they can be adjusted further on (e.g., in terms of styling and additional functionality).
There are plenty of ways of manipulating your Markdown content and various tools and methods for it. Here I just wanted to play around with one possible variant for React in particular.
Thank you for reading.









