简体   繁体   English

如何将原始 html 存储在反应组件内的变量中

[英]How to put raw html stored in variable inside a react component

I'm using React CDN.我正在使用 React CDN。 I have an index php which loads some html and a small react component.我有一个索引 php 加载一些 html 和一个小的反应组件。

From the React component, I'm trying to get an element from index.php and include it in my small React component.从 React 组件中,我试图从 index.php 中获取一个元素,并将其包含在我的小型 React 组件中。 The reason it has to go this clumsy route is because that element is being modified from some jquery scripts somewhere.它之所以要 go 这条笨拙的路线是因为该元素正在从某处的某些 jquery 脚本中修改。

So, what I did is to use getElementById to get the element and store it in a variable.所以,我所做的是使用 getElementById 来获取元素并将其存储在变量中。

The problem I'm facing is that once I use it in my React component, it gets converted to a string and the output is like this [object SVGSVGElement] .我面临的问题是,一旦我在我的 React 组件中使用它,它就会转换为字符串,并且 output 就像这样[object SVGSVGElement]

I have tried React's _dangerouslySetInnerHTML and ReactHTMLParser and even putting it directly.我已经尝试过 React 的 _dangerouslySetInnerHTML 和 ReactHTMLParser 甚至直接把它放了。

Here's what I have when logged before and after adding it to my React component这是我在将其添加到我的 React 组件之前和之后记录的内容

Before:前: 在此处输入图像描述

After:后: 在此处输入图像描述

Relevant code相关代码

In React component constructor在 React 组件构造函数中

const dailyChart = document.getElementById('dailySa2Box')
this.dailychart.push(dailyChart);

In render在渲染中

<React.Fragment >
   {
      this.dailychart.map(chart=>(
         HTMLReactParser(chart)
      ))
    }
 </React.Fragment>

This works.这行得通。

{
    this.dailychart.map(chart=>(
       HTMLReactParser(chart.outerHTML)
    ))
}

It gives HTMLReactParser the string it needs without compromising the element.它为 HTMLReactParser 提供了它需要的字符串,而不会影响元素。

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

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