简体   繁体   English

是否可以在不使用 dangerouslySetInnerHtml 的情况下渲染 HTML

[英]Is it possible to render HTML in react without using dangerouslySetInnerHtml

Long story short I have strings that contain HTML which are downloaded before a production build from a secure server.长话短说,我有包含 HTML 的字符串,这些字符串是在从安全服务器进行生产构建之前下载的。 The framework we're using is React with Gatsby and typically you would just do the following:我们使用的框架是 React with Gatsby,通常您只需执行以下操作:

<div dangerouslySetInnerHTML={{__html: '<p>My Stuff</p>'}}></div>

Which works fine, the main problem is for SEO purposes we want the html to be compiled into html rather than being rendered with Javascript.效果很好,主要问题是出于 SEO 目的,我们希望将 html 编译成 html 而不是用 Javascript 呈现。

Seeing as it's already HTML is there a way I can disable the protection inside react so these strings aren't escaped by default and become regular HTML in the production build?既然它已经是 HTML,有没有一种方法可以禁用 react 内部的保护,这样这些字符串就不会在默认情况下被转义并成为生产构建中的常规 HTML?

Not practically, no.不实际,不。 It's important to distinguish a <div> in a .jsx file from a <div> in html.重要的是要区分是很重要<div>从在.jsx文件<div>在HTML中。 If you look at the compiled output of the jsx, it will reveal what's really going on.如果您查看 jsx 的编译输出,它将揭示真正发生了什么。 If you transpile a react component containing <div>Hello world</div> , you'll get a function call along the lines of _react.createElement("div", null, "Hello world") .如果你转译一个包含<div>Hello world</div>的 react 组件,你会得到一个_react.createElement("div", null, "Hello world")的函数调用。 Anything that is put into the body of the div will also be inserted into it as JSX elements.放入 div 主体的任何内容也将作为 JSX 元素插入其中。 The dangerouslySetInnerHtml prop is there to tell React that the value is actual html that you trust.危险的SetInnerHtml 道具用于告诉 React 该值是您信任的实际 html。 However, even if there were an option other than dangerouslySetInnerHtml, I don't believe it would solve your problem as anything inside your react is going to have to wait for client side rendering.但是,即使除了危险的SetInnerHtml 之外还有其他选项,我也不相信它会解决您的问题,因为您的反应中的任何内容都将不得不等待客户端渲染。

Now, I'm no SEO expert, but if it's crucial to have content on the page before you render it, you may be able to send in the original html in a hidden element.现在,我不是 SEO 专家,但是如果在呈现页面之前在页面上包含内容至关重要,那么您可以将原始 html 发送到隐藏元素中。 If your original markup has <div id="app">Some seo stuff</div> , React will replace the content of the div when it renders it.如果你的原始标记有<div id="app">Some seo stuff</div> ,React 会在渲染时替换 div 的内容。

I think what you need is SSR;我认为你需要的是 SSR; React allows this feature You could install a server adapter (maybe expressjs) and use it to render a first screen (which is most likely SEO related) and once the client side is ready to render, it would hydrate the pages. React 允许此功能您可以安装一个服务器适配器(可能是 expressjs)并使用它来渲染第一个屏幕(这很可能与 SEO 相关),一旦客户端准备好渲染,它就会对页面进行水合。 Since you have the HTML gotten from a server, I think this is best route to go由于您有从服务器获取的 HTML,我认为这是最好的方法

var stringToHTML = function(htmlContent) {
  var dom = document.getElementById('integrationMessage');
  dom.innerHTML = htmlContent;
  return dom;
};
stringToHTML(res.data.message);
<div id = "integrationMessage"></div>

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

相关问题 React Render HTML代码(dangerouslySetInnerHTML?) - React Render HTML Code (dangerouslySetInnerHTML?) 在 React JS 中渲染 SVGSVGElement 没有危险地SetInnerHtml - Render SVGSVGElement in React JS without dangerouslySetInnerHtml 反应使用危险的SetInnerHTML 渲染带有 html 标签的 json - react use dangerouslySetInnerHTML to render json with html tags 用React.js组件替换部分html字符串并渲染(没有dangerouslySetInnerHTML) - Replace part of html string with React.js component and render (without dangerouslySetInnerHTML) 在没有div的情况下使用React的危险SetInnerHTML吗? - Using React's dangerouslySetInnerHTML without the div? 使用危险的SetInnerHTML在React中插入完整的HTML - Inserting complete HTML in React using dangerouslySetInnerHTML 在React中危险地渲染带有JSX表达式的HTML字符串SetInnerHTML() - Render HTML string w/JSX expression in React dangerouslySetInnerHTML() 在React.js中包装HTML时避免使用危险地使用SetInnerHTML - Avoid using dangerouslySetInnerHTML when wrapping HTML in React.js React 使用预先确定的组件在字符串上添加 HTML 标签(替代危险的SetInnerHTML) - React add HTML tags on string using predetermined components (alternative to dangerouslySetInnerHTML ) 清理HTML字符串而不使用dangerouslySetInnerHTML进行长度检查 - Sanitize HTML String without using dangerouslySetInnerHTML for length check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM