0

I'm happy to move to another library if needed but react-markdown seems to have good reports, however, im getting the below error when I try to use it with typescript in a functional component.

import React from 'react';
import Markdown from 'react-markdown';

const Home: React.FC = () => {

  const getMarkdown = async () => {
    const markdown = await fetch('../markdown/home.md');
    const text = await markdown.text();
    return text;
  }
  const src = getMarkdown();

  return (    
      <div className='max-width'>
          <span className='body'>
            <Markdown source={src} />
          </span>
      </div>
  );
}

export { Home };

the error I get is

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<ReactMarkdownProps>): ReactMarkdown', gave the following error.
    Type 'Promise<Response>' is not assignable to type 'string'.
  Overload 2 of 2, '(props: ReactMarkdownProps, context?: any): ReactMarkdown', gave the following error.
    Type 'Promise<Response>' is not assignable to type 'string'.  TS2769

2
  • src is a promise not a string. Commented Oct 21, 2019 at 9:10
  • Thank's Joseph I added an await and got more errors Commented Oct 21, 2019 at 9:17

1 Answer 1

1

You can use react hooks

const Home = () => {
const [text, setText] = useState(null);
  useEffect(() => {
     async function getText() {
         const markdown = await fetch('../markdown/home.md');
         const text = await markdown.text();
         setText(text);
     }
     getText();
  }, []);
  return (<div className='max-width'>
          <span className='body'>
            <Markdown source={text} />
          </span>
      </div>);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This certainly cleared the error but the markdown doesn't render
just verify if text is getting updated with markdown contents.
When I log text I dont see any of the content from home.md instead i see html from the site <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="
My guess is that path to markdown file might be wrong, try with some online markdown file ( CORS might be an issue)
If I move the markdown folder to ./public then link to it, it works, thank you!
|

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.