React Markdown dynamically required MD file, image relative path not working

℡╲_俬逩灬. 提交于 2021-02-11 13:33:20

问题


i cannot get my image in the markdown file to render. I am requiring the markdown file in a react component, but the image in the md file is not rendering. absolute paths to online images are working, but relative paths are not.

useEffect(() => {
    fetch(require(`../../../static/markdowns/${category.file}`)).then(r => r.text()).then(text => { setMarkDown(text) })
}, [])
<ReactMarkdown escapeHtml={false} source={markDown}/>
<img src='https://picsum.photos/200/300'></img>
<img src="./personwithchart.png" alt="Text">
![](https://picsum.photos/200/300)
![](./personwithchart.png)

the picsums work but ./ or ../../../static/markdowns/file do not.

thanks


回答1:


If you're using something like Create React App and the image is in your public/ folder, you should be able to do <img src="/personwithchart.png" alt="Text">. The public/ directory should be accessible on your URL's root, therefore you don't need the ./ UNIX convention.



来源:https://stackoverflow.com/questions/60606026/react-markdown-dynamically-required-md-file-image-relative-path-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!