[英]Using marked in react
I want to use marked in reactjs as described in the reactjs docs .我想按照 reactjs 文档中的描述在reactjs中使用标记。
<div>{marked(mystring)}</div>
I use babel so I import marked like this:我使用 babel 所以我导入标记如下:
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?
我如何必须在此处导入标记,以便我可以使用它?
Here's one way to use marked
with React
:这是使用
marked
为React
的一种方法:
marked
marked
marked
in your project's package.json
file:package.json
文件中包含marked
:// package.json
{
dependencies: {
react: "^17.0.0",
marked: "^4.0.0",
},
}
marked
in your .jsx
(or related) file:.jsx
(或相关)文件中marked
导入:import { marked } from "marked";
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. dangerouslySetInnerHTML
属性使您能够使用原始 (HTML) 标记。 Make sure to take care when using this attribute , though!不过,请务必在使用此属性时小心!
If you don't want to use dangerouslySetInnerHTML
and safely render HTML.如果您不想危险地使用
dangerouslySetInnerHTML
并安全地呈现 HTML。 Try marked-react , which internally uses marked
to render the html elements as react components尝试marked-react ,它在内部使用
marked
将 html 元素呈现为反应组件
npm i marked-react
import Markdown from "marked-react";
const MarkdownComponent = () => {
return <Markdown>{rawmarkdown}</Markdown>;
};
Another alternative is react-markdown另一种选择是react-markdown
If you just want to import marked:如果您只想导入标记:
import marked from 'marked';
Then call the function in your component:然后调用组件中的函数:
marked('# Markdown');
Here is another way of using marked with React Hooks:这是另一种使用标记为 React Hooks 的方法:
- Create your MarkedConverter component
创建您的 MarkedConverter 组件
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>
}
- Create Markup function and pass the value from MarkedConverter Component
创建 Markup 函数并传递来自 MarkedConverter 组件的值
export const createMarkUp = (val) => {
return { __html: marked(val) }
}
- Finally you can import MarkedConverter Component to any of your Component
最后,您可以将 MarkedConverter 组件导入到您的任何组件中
With the marked-wrapper react-marked-markdown :使用标记包装器react-marked-markdown :
import { MarkdownPreview } from 'react-marked-markdown'
export default ({ post }) => (
<div>
<h1>{ post.title }</h1>
<MarkdownPreview value={ post.content }/>
</div>
)
Here's an example on how to use marked with react:下面是一个关于如何使用 marked with react 的例子:
Install marked with NPM: npm i marked安装标有 NPM:npm i marked
import it in your react app (this example is created with create-react-app), and using it example of a react component using "marked"将其导入您的 React 应用程序(此示例是使用 create-react-app 创建的),并使用它使用“标记”的 React 组件示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.