为了账号安全,请及时绑定邮箱和手机立即绑定

React - HTML 元素内部到字符串

React - HTML 元素内部到字符串

慕森王 2023-09-28 16:19:27
快速查看我正在尝试做的事情:import React, { useState, useEffect } from 'react';const MainComponent = () => {  const [element, setElement] = useState([]);  const [setThings] = useSomething();  useEffect(() => {    setThings(element);  }, [element]);};const useSomething = () => {  const [things, setThings] = useState([]);  // do stuff now that you've received things  return [setThings];};以及另一个文件中的另一个钩子。这工作得很好,并且是在 ReactJS 中使用画布的绝佳解决方案。我有一个警告被抛出,我似乎无法通过我的研究来解决它:  Line 150:7:  React Hook useEffect has a missing dependency: 'setThings'. Either include it or remove the dependency array  react-hooks/exhaustive-deps在反应文档中,它说这会导致问题,因为它正在调用具有依赖性的东西,但我不知道它们在哪里?
查看完整描述

2 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

我不确定如何标记为重复,

基本上你想将字符串呈现为 HTML,这是一种危险的做法,所以要小心。

const x = {

        titleName: "Indorction",

        heading: 'Welcome to JavaScript',

        iconName: faDotCircle,

        description: [

            `

            Do we even need to introduce <p>HTML element</p>? It’s one of the most popular programming 

            languages on the planet!

            Ever visited a website that made you think…"Hey, this website is really cool and

            `

        ],

    },


在你的渲染函数中:


<div dangerouslySetInnerHTML={{ __html: x.description }}></div>


查看完整回答
反对 回复 2023-09-28
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

JavaScript_Lessons_Objects.js


{

    titleName: "Indorction",

    heading: 'Welcome to JavaScript',

    iconName: faDotCircle,

    description: [

      `

       Do we even need to introduce <b class="hal">JavaScript</b>? It’s one of the most 

       popular programming languages on the planet!.

      `

   ],

},

课程.jsx


{ReactHtmlParser(this.props.lesson[this.state.indexDescription]["description"])}


查看完整回答
反对 回复 2023-09-28
  • 2 回答
  • 0 关注
  • 67 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信