如何将字符串转换为 HTML 不起作用

huangapple go评论62阅读模式
英文:

How do I convert a string to HTML won't work

问题

以下是翻译好的部分:

这段代码有什么问题?最初,我尝试渲染数组的元素。但它只显示为字符串,而不是应该显示为 HTML。所以我尝试使用来自以下代码的转换方法:

https://www.learnbestcoding.com/post/84/string-to-html-in-react-js

第二种方法,使用 HTML-react-parser npm 包。但不幸的是,我遇到了以下问题:

已编译出现问题:

错误

[eslint]
src/App.js
第 4 行:36: 'string' 未定义 no-undef
第 9 行:1: 模块体中的导入;重新排序到顶部 import/first
第 10 行:1: 模块体中的导入;重新排序到顶部 import/first
第 11 行:1: 模块体中的导入;重新排序到顶部 import/first
第 23 行:5: 'arrs_list' 未定义 no-undef
第 32 行:11: 'html' 未定义 no-undef

以下是 App.js 文件的一部分:

import React, { useEffect, useState } from "react";
import parse from 'html-react-parser';
export const StringToHtml = () =>{
  const [html, setHtml] = useState<string>("")
  useEffect(() => {
    setHtml("<div>Html 存储为字符串</div>")
  }, [html])
}
function App() {
    const [val, setVal] = useState(0);
    const word = 1;
        const [arrs_list,setarrs]=useState(['<input type="text"/>'])
  return (
      <div className="App">
 ...
     <br></br>
      {arrs_list.map( (arr,index)=>
          (
          <p key={index}>{arr}</p>
          )   
      )}

     <>
      {parse(html)}
      </>
      <div></div>
      </div>
    
      );
       }
export default App;

希望这有助于你解决问题。如果你有任何其他问题,请随时提出。

英文:

tWhat is wrong with this code? Originally I had the element of an array render. But it was just diplaying as a string not as HTML as it should so I tried to convert using code from,

https://www.learnbestcoding.com/post/84/string-to-html-in-react-js

the second type, Using HTML-react-parser npm package. But unfortunately I'm getting

Compiled with problems:X

ERROR

    [eslint] 
    src/App.js
  Line 4:36:   &#39;string&#39; is not defined                   no-undef
  Line 9:1:    Import in body of module; reorder to top  import/first
  Line 10:1:   Import in body of module; reorder to top  import/first
  Line 11:1:   Import in body of module; reorder to top  import/first
  Line 23:5:   &#39;arrs_list&#39; is not defined                no-undef
  Line 32:11:  &#39;html&#39; is not defined                     no-undef

Here is part of the App.js file.

import React, { useEffect, useState } from &quot;react&quot;;
import parse from &#39;html-react-parser&#39;;
export const StringToHtml = () =&gt;{
  const [html, setHtml] = useState&lt;string&gt;(&quot;&quot;)
  useEffect(() =&gt; {
    setHtml(&quot;&lt;div&gt;Html stored as a string&lt;/div&gt;&quot;)
  }, [html])
}
function App() {
    const [val, setVal] = useState(0);
    const word = 1;
        const [arrs_list,setarrs]=useState([&#39;&lt;input type=&quot;text&quot;/&gt;&#39;])
  return (
      &lt;div className=&quot;App&quot;&gt;
 ...
    	 &lt;br&gt;&lt;/br&gt;
    	  {arrs_list.map( (arr,index)=&gt;
    	      (
    		  &lt;p key={index}&gt;{arr}&lt;/p&gt;
    	      )   
    	  )}

    	 &lt;&gt;
    	  {parse(html)}
    	  &lt;/&gt;
    	  &lt;div&gt;&lt;/div&gt;
          &lt;/div&gt;
    
    	  );
           }
    export default App;

As the site suggests I installed html-react-parser in the terminal.

答案1

得分: 1

你可以使用 dangerouslySetInnerHTML:

<div style={{ display: 'contents' }} dangerouslySetInnerHTML={{ __html: html }}></div>
英文:

You can use dangerouslySetInnerHTML:

&lt;div style={{display: &#39;contents&#39;}} dangerouslySetInnerHTML={{__html: html}}&gt;&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月19日 07:02:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定