Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using marked in react

Tags:

reactjs

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?

like image 511
Lorim Avatar asked Jan 08 '16 21:01

Lorim


People also ask

Why is this undefined In react event handler?

This is because when you use an arrow function, the event handler is automatically bound to the component instance so you don't need to bind it in the constructor. When you use an arrow function you are binding this lexically.

What is markdown to JSX?

markdown-to-jsx is an easy-to-use markdown component that takes Github-flavored Markdown (GFM) and makes native JSX without dangerous hacks.


3 Answers

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


like image 52
summea Avatar answered Oct 17 '22 19:10

summea


If you just want to import marked:

import marked from 'marked';

Then call the function in your component:

marked('# Markdown');
like image 28
SeanMcP Avatar answered Oct 17 '22 20:10

SeanMcP


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
like image 2
Roshith R Avatar answered Oct 17 '22 20:10

Roshith R