简体   繁体   English

为什么反应不编译 state 中存在的 HTML

[英]Why does react not compile HTML present in a state

I set a default value of a state to be <b> Hey </b> .我将 state 的默认值设置为<b> Hey </b> Now when I rendered this state on the UI it printed the string instead of Hey wrote in bold.I want to know why it is not working.现在,当我在 UI 上渲染这个 state 时,它打印了字符串而不是用粗体写的 Hey。我想知道它为什么不起作用。 Why react is not able to interpret the html tag and show the appropriate output为什么 react 无法解释 html 标签并显示相应的 output

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [html, setHtml] = useState("<b>Hey</b>");
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div>{html}</div>
    </div>
  );
}

Output:- Output:-

在此处输入图像描述

Was expecting the output to be Hey written in bold.期待 output 是用粗体写的嘿。

Here's the codesandbox link for better understanding:-https://codesandbox.io/s/heuristic-chaum-vo6qt?file=/src/App.js这是代码框链接,以便更好地理解:-https://codesandbox.io/s/heuristic-chaum-vo6qt?file=/src/App.js

Thank you.谢谢你。 I just want to know why react is not able to render the HTML tag as HTML tag instead of printing it out.我只是想知道为什么 react 不能将 HTML 标签呈现为 HTML 标签而不是打印出来。

Because you are rendering a string, not HTML.因为您正在渲染一个字符串,而不是 HTML。 If you want to render stringified HTML then use dangerouslySetInnerHTML , use caution what you pass through, in other words, you may want to run the string through a DOM purifier first.如果你想渲染字符串 HTML 然后使用dangerouslySetInnerHTML ,小心你通过什么,换句话说,你可能想先通过 DOM 净化器运行字符串。

export default function App() {
  const [html, setHtml] = useState("<b>Hey</b>");
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div dangerouslySetInnerHTML={{ __html: html}} />
    </div>
  );
}

编辑冷淡的tdd-ftchp

在此处输入图像描述

You are setting the value of html as "<b>Hey</b>" which is a string string that's why it renders that as it is.您将html的值设置为"<b>Hey</b>" ,这是一个字符串字符串,这就是它按原样呈现的原因。 You can directly assign html to the variable like so:您可以像这样直接将 html 分配给变量:

  const [html, setHtml] = useState(<b>Hey</b>);

It's a string and not HTML, to fix that maybe you can insert it in the div as innerHTML ie.它是一个字符串而不是 HTML,以解决这个问题,也许你可以将它作为innerHTML插入到 div 中,即。

document.querySelector(".divClassName").innerHTML = html

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

相关问题 为什么放置一个 div 标签允许这个 React 代码编译? - Why does putting a div tag allow this React code to compile? 为什么<textarea>在 React 中接受一个 value 属性,但在 HTML 中,它不接受? - Why does <textarea> in React accept a value attribute but in HTML, it does not? 当链接文本出现在 html 中时,为什么这会给我一个错误提示 NoSuchElementException? - Why does this give me an error saying NoSuchElementException when the link text is present in the html? 为什么只有其他所有HTML元素都会对“点击”做出反应? - Why does only every other HTML-element react to the “click”? 为什么 HTML 脚本标签不加载反应组件? - Why does HTML script tag not load a react component? 为什么React只允许一部分HTML实体? - Why does React only allow a subset of HTML entities? 在浏览器中编译用户 HTML 并使用 React 显示结果 - Compile user HTML in browser and display results with React webpack4不编译所有的html文件? - webpack4 does not compile all html files? Swift 3 NSURL html错误无法编译 - Swift 3 NSURL html Error does not compile 在 React 中 - 为什么重新渲染 html 输入元素的“值”属性会更新字段中的值而不是 DOM API - In React - Why does rerendering the “value” attribute of an html input element update the value in the field when is does not with the DOM API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM