如何将 Markdown 文件加载到 React 组件中?
IT技术
reactjs
markdown
2021-04-21 06:06:11
6个回答
我首先像这样导入它:
import marked from "marked";
然后我在 React 的componentDidMount
事件中获取我的 *.md 文件并使用marked(text)
(text
响应在哪里)将其存储在我的组件状态中:
componentDidMount() {
const readmePath = require("./Readme.md");
fetch(readmePath)
.then(response => {
return response.text()
})
.then(text => {
this.setState({
markdown: marked(text)
})
})
}
...最后我使用dangerouslySetInnerHTML
属性在页面上呈现它:
render() {
const { markdown } = this.state;
return (
<section>
<article dangerouslySetInnerHTML={{__html: markdown}}></article>
</section>
)
}
一个完整的工作示例react-markdown
:
import React, { Component } from 'react'
import ReactMarkdown from 'react-markdown'
import termsFrPath from './Terms.fr.md'
class Terms extends Component {
constructor(props) {
super(props)
this.state = { terms: null }
}
componentWillMount() {
fetch(termsFrPath).then((response) => response.text()).then((text) => {
this.setState({ terms: text })
})
}
render() {
return (
<div className="content">
<ReactMarkdown source={this.state.terms} />
</div>
)
}
}
export default Terms
您应该使用react-markdown而不是接受的答案,此解决方案不使用dangerouslySetInnerHTML
.
应用程序.js
import React, { Component } from 'react';
import AppMarkdown from './App.md';
import ReactMarkdown from 'react-markdown';
class App extends Component {
constructor() {
super();
this.state = { markdown: '' };
}
componentWillMount() {
// Get the contents from the Markdown file and put them in the React state, so we can reference it in render() below.
fetch(AppMarkdown).then(res => res.text()).then(text => this.setState({ markdown: text }));
}
render() {
const { markdown } = this.state;
return <ReactMarkdown source={markdown} />;
}
}
export default App;
应用程序.md
# React & Markdown App
* Benefits of using React... but...
* Write layout in Markdown!
markdown-to-jsx提供了非常有效的功能来与 React 组件中的 Markdown 交互。
它允许使用您的自定义组件替换/覆盖任何 HTML 元素以进行降价,这里是 doc。
import React, { Component } from 'react'
import Markdown from 'markdown-to-jsx';
import README from './README.md'
class PageComponent extends Component {
constructor(props) {
super(props)
this.state = { md: "" }
}
componentWillMount() {
fetch(README)
.then((res) => res.text())
.then((md) => {
this.setState({ md })
})
}
render() {
let { md } = this.state
return (
<div className="post">
<Markdown children={md}/>
</div>
)
}
}
export default PageComponent
21 年 8 月 2 日编辑
功能组件
const PageComponent = ()=> {
let [ content, setContent] = useState({md: ""});
useEffect(()=> {
fetch(README)
.then((res) => res.text())
.then((md) => {
setContent({ md })
})
}, [])
return (
<div className="post">
<Markdown children={content.md}/>
</div>
)
}
与@Xing-Han-Lu 的回答类似,但具有react Markdown。该概念用于useEffect
加载文件,然后使用useState
钩子将其添加到状态reactMarkdown
import React, { useState, useEffect } from "react";
import ReactMarkdown from "react-markdown";
import file from "./md/posts.md";
export default function () {
const [markdown, setMarkdown] = useState("");
useEffect(() => {
fetch(file)
.then((res) => res.text())
.then((text) => setMarkdown(text));
}, []);
return (
<>
<ReactMarkdown source={markdown} />
</>
);
}
其它你可能感兴趣的问题