简体   繁体   English

封装React应用的最佳方法是什么?

[英]What is the best way to encapsulate React app?

I am trying to encapsulate a React application in a Web component's Shadow Root or an iFrame to build a widget for a Chatbot messenger like Intercom widget. 我想封装在一个Web组件的影子根iframe 中作出反应的应用程序创建一个小部件的聊天机器人信使像对讲机部件。

I´m using: 我正在使用:

React 16.8.6
Typescript 3.5.3
Redux 4.0.4 
Styled-components 4.3.2 
Material-UI 4.3.3

I based my Web component choice on this React official doc (that didint mention any of my issue) . 我基于这个 React官方文档(没有提到我的任何问题)来选择Web组件。

First, I encountered some style issue , with both styled-components and Material-UI, because they both put their styles in the light DOM header. 首先,我遇到了一些样式问题 ,即styled-components和Material-UI,因为它们都将样式放在了轻量的DOM标头中。 But, I figured out how to deal with both issues (I can help anyone with the same problems). 但是, 我弄清楚了如何处理这两个问题(我可以帮助遇到相同问题的任何人)。 [SOLVED] [解决了]

Then my components are rendered without a problem, but events are not working , After some research I found that when you render a react component inside Shadow DOM events will not be dispatched to react. 然后,我的组件得以正确呈现,但是事件不起作用 ,经过一些研究,我发现在Shadow DOM中呈现React组件时,事件不会被分派来进行响应。 Ie when a user clicks on your react component nothing happens. 即,当用户单击您的反应组件时,什么也不会发生。 so i used this repo to fix this issue and retarget and dispatch events to React. 所以我用这个仓库来解决这个问题, 并将事件重新定向和调度到React。 [PARTIALLY-SOLVED] [部分解决]

But unfortunately, this is not enough, because some events like onChange don't get fixed, and some depth like Material-UI add their own events, The problem is in React it self check this issue and they are not so motivated to fix it in a near date.[ISSUE] 但是不幸的是,这还不够,因为某些事件(例如onChange没有得到修复,而某些深度(例如Material-UI)添加了自己的事件,问题在于React会自行检查此问题,而他们并没有那么积极地去解决它在临近的日期。[问题]

So I decided to move to another encapsulation alternative , the iFrame . 因此,我决定转向另一个封装替代方案 iFrame I didn't find a helpful tuto, repo or thread yet and all existing npm modules aren't updated neither optimized for Typescript. 我还没有找到有用的tuto,repo或线程,并且所有现有的npm模块都没有更新,也没有针对Typescript进行优化。 I need a clear way to encapsulate my app in the #document of the iFrame . 我需要一种明确的方法来将我的应用程序封装在iFrame#document中。

Please, propose a repo. 请提出一个回购协议。 or give me any idea of implementation, any idea could be very helpful. 或给我任何实现的想法,任何想法都可能会很有帮助。

Finally, I did encapsulate my app in the #document of the iframe successfully. 最后,我确实将我的应用程序成功封装在iframe#document中。

// Create iframe.
const iFrame: any = document.createElement("iframe")
document.body.appendChild(iFrame)
// Create react entry point.
const mountPoint = document.createElement("div")
iFrame.contentWindow.document.body.appendChild(mountPoint)
ReactDOM.render(<App />, mountPoint)

I just forgot to point on the x.contentWindow.document.body element, instead, I pointed directly on the iframe . 我只是忘记指向x.contentWindow.document.body元素,而是直接指向iframe

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

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