简体   繁体   English

反应:如何将 html 作为道具传递

[英]React: How to pass html as prop

I want to create a component that I'll use as <Component htmldata=? />我想创建一个组件,用作<Component htmldata=? /> <Component htmldata=? /> in a mdx file. <Component htmldata=? />在一个 mdx 文件中。

That component will be like that: ...etc render{return{ <div> {this.props.htmldata} </div>}} .该组件将是这样的: ...etc render{return{ <div> {this.props.htmldata} </div>}} To what must I assign the htmldata property to return some HTML inside the div.我必须分配htmldata属性以返回 div 内的一些 HTML。 My aim is to use the same component easily in the mdx file multiple times with different htmldata without having to create another component.我的目标是使用不同的htmldata在 mdx 文件中轻松地多次使用相同的组件,而无需创建另一个组件。

Edit: i recommend using a function that returns html and is binded and passed as a prop.编辑:我建议使用一个返回 html 并被绑定并作为道具传递的函数。 example:例子:

constructor(props) {
super(props);

this.state = {

  linkfunction: this.props.link.bind(this)
}

} this function is passed as a prop like that:这个函数是作为一个道具传递的:

<LiveCode title="MyhtmlComponent" link={function(){return <p>the html i pass </p> ;}} />

If you want put JSX(html) throught props, usually is better use props.childern如果你想把 JSX(html) 通过道具,通常最好使用 props.childern

React js - documentation (about props childern) React js - 文档(关于 props childern)

or you can use dangerouslySetInnerHTML或者你可以使用危险的SetInnerHTML

Reactjs - dangerouslysetinnerhtml Reactjs - 危险地setinnerhtml

You can use dangerouslySetInnerHTML but that is not recommended您可以使用dangerouslySetInnerHTML但不建议这样做

Read more here在这里阅读更多

https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM