简体   繁体   English

在React中动态呈现外部HTML / React组件

[英]Rendering external HTML/React components dynamically in React

Is it possible to take HTML/JSX content from an external source and render it in dynamically in React? 是否可以从外部源获取HTML / JSX内容并在React中动态呈现它? In our case we want to take content from the Wordpress API and render it on both the client and the server (we're using NextJS) 在我们的例子中,我们想从Wordpress API获取内容并在客户端和服务器上呈现它(我们使用的是NextJS)

So, the Wordpress API returns a JSON response which includes a content property which is a string of HTML/JSX. 因此,Wordpress API返回一个JSON响应,其中包含一个内容属性,该属性是一个HTML / JSX字符串。 the content would look something like this. 内容看起来像这样。

{
    content: "<div><Slider imageCount="5" galleryID="1"></Slider><span>This is an image gallery</span></div>"
}

So, as you can see it would be a mix of HTML and React components/JSX, represented as a string 因此,正如您所看到的,它将是HTML和React组件/ JSX的混合,表示为字符串

I would use Axios to make a call to get the content (on both server and client using NextJS's getInitialProps() method), then I need to render it, but i'm new to react and I can see a couple of problems. 我会使用Axios来调用内容(使用NextJS的getInitialProps()方法在服务器和客户端上),然后我需要渲染它,但我是新的反应,我可以看到一些问题。

1) In React, JSX is compiled at build time, not run time, I can't see how to get round this (It would have been easy in Angular using $compile service for example). 1)在React中,JSX是在构建时编译的,而不是运行时,我看不出如何绕过它(例如,使用$ compile服务在Angular中会很容易)。

2) As we don't know what components the content from Wordpress is going to use, we'd have to import every single one of them at the top of the page, the content may include a component or it may include a component, who knows?. 2)由于我们不知道Wordpress的内容将使用哪些组件,我们必须在页面顶部导入它们中的每一个,内容可能包含一个组件,或者它可能包含一个组件,谁知道?。

Right now, I'm thinking this isn't possible, which would mean we'd have to reconsider using React, but I'm really hoping somebody has an answer. 现在,我认为这是不可能的,这意味着我们不得不重新考虑使用React,但我真的希望有人有答案。

Any help would really be appreciated. 真的很感激任何帮助。

Interesting problem! 有趣的问题!

You should try react-jsx-parser . 你应该尝试使用react-jsx-parser I think it solves your problems. 我认为它解决了你的问题。 Not sure how it works with Next JS - I have no experience with Next JS. 不确定它如何与Next JS一起工作 - 我没有使用Next JS的经验。

Check out this sandbox: 看看这个沙盒: 编辑24r1ypp00p


You are right about all the components getting bundled. 你对捆绑的所有组件都是正确的。 There is a workaround for that. 有一个解决方法。 :) :)

Check out this sandbox: 看看这个沙盒: 编辑24r1ypp00p

I've created a dynamicComponent that expects an import promise and returns a component. 我创建了一个期望导入承诺并返回组件的dynamicComponent

I changed the way A, B and C components are imported in index.js . 我改变了在index.js中导入A,B和C组件的方式。 This way each dynamically imported component gets a separate bundle and is only requested when needed. 这样,每个动态导入的组件都会获得一个单独的包,只在需要时才会被请求。

This should solve your second problem. 这应该解决你的第二个问题。

You can refer to the below link 您可以参考以下链接

https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

For the React Components part you can render an Static HTML Markup string using 对于React Components部分,您可以使用以下方式呈现静态HTML标记字符串

https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup

But be sure that the html you get is from an authentic source and will not contain any malicious scripts 但请确保您获得的html来自可靠的来源,并且不包含任何恶意脚本

You can Render external HTML/React components dynamically in React like this, simply in these quotation , content: `<div>${<Slider imageCount="5" galleryID="1"></Slider>}<span>This is an image gallery</span></div> 您可以像这样在React中动态渲染外部HTML / React组件,只需在这些引用中, content: `<div>${<Slider imageCount="5" galleryID="1"></Slider>}<span>This is an image gallery</span></div>

ends with these ` sign. 以这些'标志结束。

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

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