简体   繁体   English

将 React 组件插入到具有 html 内容的字符串变量中

[英]Insert React component into a String variable with html content

I need to insert a React component into a String variable that contains html content.我需要将 React 组件插入到包含 html 内容的字符串变量中。 This variable will be render with dangerouslySetInnerHTML.此变量将使用 dangerouslySetInnerHTML 呈现。

I replace my placeholder ",#ShareButton" with the content of my React component, but it renders [object Object] instead of the component itself我将占位符“,#ShareButton”替换为我的 React 组件的内容,但它呈现 [object Object] 而不是组件本身

React component:反应组件:

const ShareThis = ({ text }) => {
  return (
    <div class="one-line">{text}</div>
)}

export default ShareThis

var content (string variable with html content) var 内容(具有 html 内容的字符串变量)

<p>Text 1</p>
!#ShareButton
<p>Text 2</p>

Page:页:

const htmlcontent = content.replace(/!#ShareThis/g,
  <ShareThis
    text="Hello"
  />)

return (
  <div dangerouslySetInnerHTML={{ __html: htmlcontent }} />
)

Result:结果:

Text 1
[object Object]
Text 2

Do you know how to insert a React component into a String variable with html content?你知道如何将 React 组件插入到具有 html 内容的 String 变量中吗?

You can't render React component as innerHTML because its an object, don't confuse between JSX and HTML.你不能将 React 组件渲染为innerHTML ,因为它是 object,不要混淆JSX和 HTML。

To solve it your need content to be a valid HTML .要解决它,您需要content成为有效的HTML

Can be done by making ShareThis a function returning a valid HTML as a string:可以通过将ShareThis设为 function 返回有效的HTML作为字符串来完成:

const ShareThis = ({ text }) => {
  return `<div class="one-line">${text}</div>`;
};

const content = `
<p>Text 1</p>
!#ShareButton
<p>Text 2</p>
`;

const htmlcontent = content.replace(
  /!#ShareButton/g,
  ShareThis({ text: "Hello" })
);

const App = () => {
  return <div dangerouslySetInnerHTML={{ __html: htmlcontent }} />;
};

Note that you have a typo in regex expression: /!#ShareButton/请注意,您在正则表达式中有错字: /!#ShareButton/

编辑 green-sound-n5fvc

I don't know what use case are you trying to solve using this but you can edit your shareThis function like this to return html as a string to use it as innerHtml later:我不知道您要使用此解决什么用例,但您可以像这样编辑您的shareThis function 以将 html 作为字符串返回,以便稍后将其用作innerHtml

const ShareThis = ({ text }) => {return (
   `<div class="one-line">${text}</div>`
)}
export default ShareThis

This should work..这应该工作..

Another possible alternative would be to use the utility React HTML Parser which can convert HTML strings into react components ( JSX ).另一种可能的替代方法是使用实用程序React HTML Parser ,它可以将 HTML 字符串转换为反应组件( JSX )。

Building on the example provided by Dennis:以 Dennis 提供的示例为基础:

import React from "react";
import ReactHtmlParser from "react-html-parser";

const ShareThis = ({ text }) => {
  return `<div class="one-line">${text}</div>`;
};

const content = `
<p>Text 1</p>
!#ShareButton
<p>Text 2</p>
`;

const htmlcontent = content.replace(
  /!#ShareButton/g,
  ShareThis({ text: "Hello" })
);

export default function App() {
  return <div>{ReactHtmlParser(htmlcontent)}</div>;
}

Working example工作示例

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

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