Rumah > Soal Jawab > teks badan
P粉4861381962023-08-18 00:37:40
Adakah ini yang anda mahukan?
import React, {renderToString} from "react"; import "./style.css"; import * as ReactDOMServer from 'react-dom/server'; export default function App() { return ( <StringReplacement /> ); } class StringReplacement extends React.Component { render() { //这可以是从所见即所得编辑器中作为字符串的任何有效的HTML const originalString = "<div>some text</div><strong>some strong text {{response}}</strong>"; // 用JSX内容替换'{{response}}' const jsxReplacement = <span onClick={ () => alert('')}>JSX content here</span>; const replacedString = originalString.replace('{{response}}', ReactDOMServer.renderToString(jsxReplacement)); return ( <div> <div dangerouslySetInnerHTML={{ __html: replacedString }} /> </div> ); } } export default StringReplacement;