0

I would like to create a method in React using markdown, simply to receive a string like "Hello World!" and render it in html. At the moment my program is returning the string "Hello World!" without rendering it in html.


    import React from 'react';
    import ReactMarkdown from "react-markdown";

       class App extends React.Component 
       {
         constructor(props){
         super(props)
         this.changeToMarkdown = this.changeToMarkdown.bind(this)
       }



        changeToMarkdown(str)
        {
        var markdown = require( "markdown" ).markdown;
         return markdown.toHTML(str);
        }


       render()
       {

          return (
             <div>
                <p>{this.changeToMarkdown("Hello *World*!")}</p>
             </div>
            )
        } 
    }    

    export default App


1 Answer 1

2

You're already importing react-markdown; why not use it?

render() {
  return (
    <div>
      <ReactMarkdown>{"Hello *World*!"}</ReactMarkdown>
    </div>
  );
}

Here's a simple CodePen that shows this in action.

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

2 Comments

Can I incorporate React Markdown components into another component? I think the answer is yes.
I'm not sure exactly what you're asking, but a react markdown component is just like any other component and it can indeed be composed with or into other components.

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.