31

I want to use marked in reactjs as described in the reactjs docs.

<div>{marked(mystring)}</div>

I use babel so I import marked like this:

import { marked } from 'marked';

Unfortunately the import statement does not work. marked is not defined. How do I have to import marked here, so that I can use it?

5 Answers 5

46

Here's one way to use marked with React:

  1. Ensure that you've installed marked
  2. Include marked in your project's package.json file:
// package.json
{
  dependencies: {
    react: "^17.0.0",
    marked: "^4.0.0",
  },
}
  1. Import marked in your .jsx (or related) file:
import { marked } from "marked";
  1. Use the dangerouslySetInnerHTML approach as shown in the example below:
import React from "react";
import { marked } from "marked";

class MarkdownExample extends React.Component {
  getMarkdownText() {
    var rawMarkup = marked.parse("This is _Markdown_.");
    return { __html: rawMarkup };
  }
  render() {
    return <div dangerouslySetInnerHTML={this.getMarkdownText()} />;
  }
}

The dangerouslySetInnerHTML attribute gives you the ability to work with raw (HTML) markup. Make sure to take care when using this attribute, though!

Alternative (Safe)

If you don't want to use dangerouslySetInnerHTML and safely render HTML. Try marked-react, which internally uses marked to render the html elements as react components

npm i marked-react
import Markdown from "marked-react";

const MarkdownComponent = () => {
  return <Markdown>{rawmarkdown}</Markdown>;
};

Another alternative is react-markdown


Sign up to request clarification or add additional context in comments.

2 Comments

Please change the npm library from 'react-marked' to just 'marked' in point no. 1. Lost a lot of time due to that.
OP said they "want to use marked in reactjs" and you listed react-markdown at the end. Just FYI, react-markdown does NOT use marked under the hood (it uses remark).
3

With the marked-wrapper react-marked-markdown:

import { MarkdownPreview } from 'react-marked-markdown'

export default ({ post }) => (
  <div>
    <h1>{ post.title }</h1>
    <MarkdownPreview value={ post.content }/>
  </div>
)

Comments

3

Here is another way of using marked with React Hooks:

  1. Create your MarkedConverter component
import { useState } from 'react'
import marked from 'marked'

export const MarkedConverter = () => {
  const [markedVal, setMarkedVal] = useState(
    '# Welcome to my React Markdown Previewer!'
  )
  return <div dangerouslySetInnerHTML={createMarkUp(markedVal)}></div>
}
  1. Create Markup function and pass the value from MarkedConverter Component
export const createMarkUp = (val) => {
 return { __html: marked(val) }
}
  1. Finally you can import MarkedConverter Component to any of your Component

Comments

2

If you just want to import marked:

import marked from 'marked';

Then call the function in your component:

marked('# Markdown');

Comments

0

Here's an example on how to use marked with react:

  1. Install marked with NPM : npm i marked

  2. import it in your react app (this example is created with create-react-app), and using it example of a react component using "marked"

  3. result in the browser : preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.