Level 58
To render markdown in React with Inertia, you can use a library like react-markdown. Here's an example of how to use it:
- Install the library:
npm install react-markdown
- Import the library in your React component:
import ReactMarkdown from 'react-markdown';
- Pass the markdown content as a prop to the ReactMarkdown component:
<ReactMarkdown source={markdownContent} />
Here, markdownContent is the variable that contains the markdown content you want to render.
- If you're using Inertia, you can pass the markdown content as a prop from your controller to your view, and then to your React component:
public function show(Post $post)
{
$markdownContent = $post->content;
return Inertia::render('Post', [
'markdownContent' => $markdownContent,
]);
}
Then, in your view:
<Layout>
<Post markdownContent={props.markdownContent} />
</Layout>
And finally, in your Post component:
import ReactMarkdown from 'react-markdown';
const Post = ({ markdownContent }) => {
return (
<div>
<ReactMarkdown source={markdownContent} />
</div>
);
};
export default Post;