简体   繁体   English

将事件从jQuery插件转发到React组件?

[英]Forward events from a jQuery plugin to a React component?

Let's say I have a project that is mostly written in React and I want to add a dropdown from Semantic UI . 假设我有一个主要用React编写的项目,我想从Semantic UI添加一个下拉列表 Is it possible to interop them somehow? 是否可以通过某种方式互操作? I mean that there should be a React component that on componentDidMount calls $('.ui.dropdown').dropdown() and then somehow forwards jQuery events to React, which keeps the state in sync. 我的意思是应该有一个React组件在componentDidMount调用$('.ui.dropdown').dropdown() ,然后以某种方式将jQuery事件转发到React,从而使状态保持同步。 Is that possible to do? 那有可能吗? I understand that it probably will cause performance issues, but it still might be useful in case there's no React component that does the same thing as an existing jQuery plugin. 我知道这可能会导致性能问题,但是在没有与现有jQuery插件功能相同的React组件的情况下,它仍然可能有用。

There is no silver bullet afaik, jQuery "plugins" tend to do a lot of DOM manipulations that won't play well with React's virtual DOM diffing. 没有灵丹妙药,jQuery“插件”往往会执行很多DOM操作,这些操作无法与React的虚拟DOM差异配合。

Some plugins will also do DOM manipulation outside it's own element, f.ex modals that appends nodes into the document.body or elsewhere. 某些插件还将在其自身的元素(f.ex模态)之外进行DOM操作,这些元素将节点附加到document.body或其他地方。

If you know a little bit about the plugin and what it does, you might be able to manually create such a component "bridge" that also listens for jQuery events and calls component lifecycle methods, but I think you will have to do that manually for each plugin. 如果您对插件及其功能有所了解,则可以手动创建这样的组件“桥”,它也侦听jQuery事件并调用组件生命周期方法,但是我认为您必须手动执行此操作每个插件。

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

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