Javascript 如何在反应原生中要求下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32204313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:44:35  来源:igfitidea点击:

How to require underscore in react-native

javascriptreact-native

提问by Ilake Chang

The change log of react-native mentions https://facebook.github.io/react/blog/2015/04/17/react-native-v0.4.html

react-native 的变更日志提到 https://facebook.github.io/react/blog/2015/04/17/react-native-v0.4.html

NPM modules compatibility: There are a lot of libraries on NPM that do not depend on node/browser internals that would be really useful in React Native, such as superagent, underscore, ...

NPM 模块兼容性:NPM 上有很多不依赖节点/浏览器内部结构的库,这些库在 React Native 中非常有用,例如 superagent、下划线……

But It doesn't work for me. It is how I install, through package.json

但这对我不起作用。这是我通过 package.json 安装的方式

# package.json

 "dependencies": {
   "react-native": "*",
   "underscore": "^1.8.3"
   ...

And I indeed see it in the npm dependecy

我确实在 npm 依赖中看到了它

# npm ls
├─┬ [email protected]
|  ...
├── [email protected]
└── [email protected]

And it does work for some other react components

它确实适用于其他一些反应组件

It is how I require

这就是我的要求

var _ = require('underscore');

But it doesn't work, _is undefined

但它不起作用,_未定义

回答by Hubert Perron

If you are using ES6 module (like in ReactNative) the correct way is to use the import statement:

如果您使用 ES6 模块(如 ReactNative),正确的方法是使用 import 语句:

import _ from 'lodash'

let text = _.isUndefined(route.rightButtonText) ? 'Default value' : route.rightButtonText;

回答by eyal83

I am using lodash (underscore with more stuff) like this:

我正在使用 lodash(带有更多内容的下划线),如下所示:

  1. Add this to the package.json "lodash": "^3.10.0"

  2. In the component you need just write: var _ = require('lodash')

  1. 将此添加到 package.json "lodash": "^3.10.0"

  2. 在组件中,您只需编写: var _ = require('lodash')

And you are set.

你已经准备好了。

Here is more info on lodash if you need lodash

如果您需要lodash,这里有更多关于 lodash 的信息

回答by Asif Shiraz

To run require successfully in React, this is what I did:

为了在 React 中成功运行 require,我是这样做的:

  1. Install underscore.

    npm install underscore
    
  2. Define a dependency in package.json

    "dependencies": {
      "react": "^0.13.*",
      "underscore": "^1.8.3"
    }
    
  3. Define underscore inside the function where you want to use it.

    render() { 
      let _ = require('underscore')
      let buttonStyle = _.clone(button);
    }
    
  1. 安装下划线。

    npm install underscore
    
  2. 在 package.json 中定义依赖项

    "dependencies": {
      "react": "^0.13.*",
      "underscore": "^1.8.3"
    }
    
  3. 在要使用它的函数内定义下划线。

    render() { 
      let _ = require('underscore')
      let buttonStyle = _.clone(button);
    }
    

回答by Ilake Chang

I found the issue, the problem is

我找到了问题,问题是

I do NOT use it

我不用这个

I just require it and try to test it in console.

我只需要它并尝试在控制台中测试它。

When I use it in somewhere, like _.map([1, 2, 3], function(num){ return num * 3; })anywhere.

当我在某个地方使用它时,比如_.map([1, 2, 3], function(num){ return num * 3; })在任何地方。

Then I test it in console, it does require the library this time.

然后我在控制台中测试它,这次它确实需要库。

I am not sure this is npm require or react-native behavior ?

我不确定这是 npm require 还是 react-native 行为?

When you don't use a library, even you require it, it won't be required.

当您不使用库时,即使您需要它,也不会需要它。