简体   繁体   English

如何在 ReactJS 中将 HTML 作为道具传递

[英]How can I pass HTML as props in ReactJS

I have an HTML tag value in my state variable as我的状态变量中有一个 HTML 标记值作为

this.state = {
    value: '<p>This is a paragraph</p>'
}

I want to display this HTML content in my child component.我想在我的子组件中显示这个 HTML 内容。 I'm trying to do as我正在尝试做

<Childcomponent value={this.state.value} />

So that i can use the props to access the value inside the child component.这样我就可以使用props来访问子组件内的值。 My child component is我的子组件是

render() {
    return(
        <div>{this.props.value}</div>
    )
}

But this produces some errors.但这会产生一些错误。 How can I fix this?我怎样才能解决这个问题? Is there any other solution?还有其他解决方案吗? Thanks in advance.提前致谢。

you should use jsx for this,你应该为此使用 jsx,

const someHtml = (<p>This is a paragraph</p>)

and then pass it to your child component as prop like this (prop type is node)然后像这样将其作为道具传递给您的子组件(道具类型为节点)

<Childcomponent value={someHtml} />

and render it like any other variable {value} in the child component并像子组件中的任何其他变量{value}一样渲染它

I wouldn't recommend doing it, but if you know what you are doing, you can use dangerouslySetInnerHTML prop.我不建议这样做,但是如果您知道自己在做什么,则可以使用dangerouslySetInnerHTML SetInnerHTML 道具。 So, your ChildComponent should be something like所以,你的 ChildComponent 应该是这样的

function ChildComponent(props) {
  return <div dangerouslySetInnerHTML={props.value} />
}

But, like I said, I don't recommend doing it, as it can be vulnerable for XSS attacks.但是,就像我说的,我不建议这样做,因为它容易受到 XSS 攻击。 You can find more info in React Docs您可以在React Docs 中找到更多信息

I think the approach is wrong here.我认为这里的方法是错误的。

Firstly, state should be reserved for values that change or data coming in from an API.首先,状态应保留用于更改的值或来自 API 的数据。 I would not say that a html snippet should be part of an applications state.我不会说 html 片段应该是应用程序状态的一部分。

Eliran suggests an approach, but again whereby the child component is expecting a prop called value. Eliran 提出了一种方法,但同样,子组件期待一个名为 value 的道具。

There is also another way, which is 'children' where you can squirt in html to a child component.还有另一种方式,即“儿童”,您可以在其中将 html 喷射到子组件。

Eg例如

<Childcomponent value={somePropToPassDown}>
    <p>This is a paragraph</p>
</ChildComponent>

and in the component itself....并在组件本身....

const ChildComponent= (props) => {
    return (
        <div>
            <p>{props.value}</p>
            {props.children}
        </div>
    )
}

Have a read of this article on React Children which explains in more detail阅读这篇关于React Children 的文章,里面有更详细的解释

Don't set the value as string instead you can set the value dynamically wrapping the text with a html element.不要将值设置为字符串,而是可以设置值,使用 html 元素动态包装文本。

constructor(props) {
 super(props);
 this.state = {
  value: ''
 }
}

componentDidMount = async () => {
 this.setState({
  value: <p>This is a paragraph</p>
 })
}

By this way you can set and render the html element dynamically.通过这种方式,您可以动态设置和呈现 html 元素。

You can also use dangerously set inner html.您还可以使用危险的内部 html 设置。 Here is the link for the reference, 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