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
How to require underscore in react-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(带有更多内容的下划线),如下所示:
Add this to the package.json
"lodash": "^3.10.0"
In the component you need just write:
var _ = require('lodash')
将此添加到 package.json
"lodash": "^3.10.0"
在组件中,您只需编写:
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,我是这样做的:
Install underscore.
npm install underscore
Define a dependency in package.json
"dependencies": { "react": "^0.13.*", "underscore": "^1.8.3" }
Define underscore inside the function where you want to use it.
render() { let _ = require('underscore') let buttonStyle = _.clone(button); }
安装下划线。
npm install underscore
在 package.json 中定义依赖项
"dependencies": { "react": "^0.13.*", "underscore": "^1.8.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.
当您不使用库时,即使您需要它,也不会需要它。