简体   繁体   English

可以在同一.js文件中使用reactJS和Vanilla JavaScript吗?

[英]Can both reactJS and Vanilla JavaScript be used in the same .js file?

I am new to ReactJS. 我是ReactJS的新手。

I was wondering if I can use both vanilla JavaScript as well as reactJS in the same .js file. 我想知道是否可以在同一.js文件中同时使用普通JavaScript和reactJS。 I am aware that ReactJS is a javaScript library and not a framework. 我知道ReactJS是一个JavaScript库,而不是一个框架。 So am I right in saying that I am indeed allowed to use both vanilla JavaScript and reactJS in the same .js file? 所以我说对了,确实允许我在同一.js文件中同时使用Vanilla JavaScript和reactJS吗?

I tried searching online but was unable to get a solid answer. 我尝试在线搜索,但无法获得可靠的答案。

Appreciate your help! 感谢您的帮助!

JavaScript is JavaScript … so technically yes. JavaScript是JavaScript…从技术上讲是这样。

However, React does masses and masses of stuff with the DOM in its own special way, and if you try to use non-React JS to modify the DOM you are likely to run into conflicts. 但是,React以其自己的特殊方式对DOM进行大量处理,如果尝试使用非React JS修改DOM,则很可能会发生冲突。

Yeah, you can certainly put whatever code you need in there; 是的,您当然可以在其中放置所需的任何代码。 it's just that depending on what your code does (and I think particularly if it changes the state of your app), you may need to find a way to let React know what transpired. 只是取决于代码的功能(我认为尤其是如果它更改了应用程序的状态),您可能需要找到一种方法来让React知道发生了什么。

Otherwise, React's opinion of the current state may get out of sync with whatever changes you made, in which case subsequent code won't work as expected. 否则,React对当前状态的看法可能与您所做的任何更改都不同步,在这种情况下,后续代码将无法按预期工作。 This thinking is based mostly on my experience with Polymer, which provides a .notifyPath method to update a parent component about any changes made to its children; 这种想法主要基于我对Polymer的经验,该经验提供了一个.notifyPath方法来更新父组件有关对其子代所做的任何更改。 hopefully React has a similar mechanism you can use to keep it caught up in real time to any vanilla-flavored changes you make. 希望React具有类似的机制,您可以使用它来使它实时跟踪您所做的任何香草风味的更改。

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

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