对我的上下文感到困惑

对我的上下文感到困惑

本文介绍了nwjs reactjs,对我的上下文感到困惑.文件未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 nwjs 应用程序中,我正在使用 React 来构建我的UI.当前,反应是通过主文件index.html中的<script>标记加载的. index.html还有另一个<script>标签,该标签会加载main.js,其中包含的代码定义并呈现了我的 React 组件,并且需要(require())一些Node模块,例如"fs"和"McFly".

In my nwjs application i am using React to build my UI. Currently, React is being loaded via a <script> tag in the main file, index.html. index.html has another <script> tag which loads main.js containing code which defines and renders my React components as well as requiring (require()) a few Node modules such as "fs" and "McFly".

这一切似乎都有效,但是当我尝试使用另一个节点模块(react-inlinesvg)时,出现错误,文档未定义".

This all seems to be working, however when i try using another node module (react-inlinesvg) i get an error, "document is undefined".

一直在网上寻求帮助,我得出的结论是, React 现在认为它正在服务器上运行?奇怪的是,就像我开始使用react-inlinesvg模块之前一样,它很高兴地使用React.render(客户端渲染)渲染组件.

Having looked online for help, i have come to the conclusion that React now believes that it is being run on the server? Which is odd, as before i started using the react-inlinesvg module it was happily rendering components using React.render (clientside rendering).

如果您需要更多背景信息,请询问.

推荐答案

可能是在服务器端进行渲染,也可能是在双方进行渲染.在第二种情况下,您可以简单地嵌套导致错误的行:

It could be that you are rendering on the server side, or also that you are rendering both sides. In the second case you could simple nest the line that is causing you error with:

if (process.env.BROWSER) {
   the line causing the error
}

如果错误消失,说明您也位于服务器端!我希望这会有所帮助...

If the error disappears, it means that you are on the server side also!I hope this helps...

基本上,如果您的代码是通用(或同构,如果需要的话.). .)通过此检查,您只能在客户端执行代码,例如,要使用特定的样式表来执行此操作:

Basically if you code is universal (or isomorphic, if you want...) with this check you can execute the code only on client side, you want to do this to use a particular style-sheet for example:

if (process.env.BROWSER) {
  require("../style/main.scss");
}

自然,如果您想在服务器端进行操作,则可以检查

Naturally if you want to do stuff server-side you can check

if (!process.env.BROWSER) {

}

这篇关于nwjs reactjs,对我的上下文感到困惑.文件未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:39