简体   繁体   English

是否可以将字符串设置为 html 元素,然后在其上使用 innerhtml 进行反应?

[英]Is it possible to set a String as html element then use innerhtml on it in react?

import React from "react";

class App extends React.Component {
    render() {

        var formula = "<div> hello </div>"
        
        const div = document.createElement("div")
        div.innerHTML = formula

        return (

            <div>

                {div}

            </div>



        );
    }
}

export default App;

I get an error我收到一个错误

Error: Objects are not valid as a React child (found: [object HTMLLIElement]). If you meant to render a collection of children, use an array instead.

Basically the string has html elements I want to set inner html to the div constant.基本上字符串有 html 元素我想将内部 html 设置为 div 常量。 Right now yeah its relatively simple I just need to know if this can be done现在是的,它相对简单我只需要知道这是否可以完成

My original code has something like this littered throughout my code in recursions too我的原始代码在我的递归代码中也有类似的东西

var formula = "<div>"
formula = formula + "something"
formula = formula + "</div>

You don't need to create a element you can directly use:您不需要创建可以直接使用的元素:

   class App extends React.Component {
       render() {

           const formula =<div> hello </div>
           
          
           return (

               <div>

                   {formula}

               </div>



           );
       }
   }

   export default App; 

This cannot be done this way in React.这在 React 中不能以这种方式完成。 A component can only return a ReactElement or JSXElement.一个组件只能返回一个 ReactElement 或 JSXElement。 If for some reason you absolutely need to set HTML from a string (there are reasons for this), you have to use dangerouslySetInnerHTML which was made for that precise purpose, but exposes potential security vulnerabilities as explained in the documentation.如果出于某种原因您绝对需要从字符串中设置 HTML(这是有原因的),您必须使用为该精确目的而制作的危险的SetInnerHTML,但会暴露潜在的安全漏洞,如文档中所述。

You can accomplish this as detailed in the official example:您可以按照官方示例中的详细说明完成此操作:

import React from "react";

class App extends React.Component {
 
    createMarkup() {
       return {__html: 'Hello'};
    }

    render() {

        return (

            <div dangerouslySetInnerHTML={createMarkup()}/>

        );
    }
}

export default App;

But again, this is discouraged and I'm curious to know why you can't just return JSX markup as the other answers suggest.但同样,这是不鼓励的,我很想知道为什么你不能像其他答案所暗示的那样只返回 JSX 标记。

Why are you creating div like that?你为什么要这样创建 div ? I'm begginer in ReactJs,but I think you shouldn't touch / create HTML DOM Elements like that at all or if it is not neccesary.我是 ReactJs 的初学者,但我认为你根本不应该触摸/创建 HTML DOM 元素,或者如果它不是必需的。

You can simply just do this.你可以简单地做到这一点。

Let formula = <div> hello </div>

Return(
  <div>
    {formula}
  </div>
)

You can do it with useState.你可以用 useState 来做到这一点。

import {useState} from "react";

const App = () => {

  const [formula, setFormula] = useState("");

  setFormula(<div> 'hello' </div>);


  return(
    <div>
      {formula}
    </div>
  );
}

export default App;

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

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