The change log of react-native mentions 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, ...
But It doesn't work for me. It is how I install, through package.json
# package.json
"dependencies": {
"react-native": "*",
"underscore": "^1.8.3"
...
And I indeed see it in the npm dependecy
# npm ls
├─┬ react-native@0.8.0
| ...
├── react-native-navbar@0.7.3
└── underscore@1.8.3
And it does work for some other react components
It is how I require
var _ = require('underscore');
But it doesn't work, _
is undefined
If you are using ES6 module (like in ReactNative) the correct way is to use the import statement:
import _ from 'lodash'
let text = _.isUndefined(route.rightButtonText) ? 'Default value' : route.rightButtonText;
I am using lodash (underscore with more stuff) like this:
Add this to the package.json "lodash": "^3.10.0"
In the component you need just write: var _ = require('lodash')
And you are set.
Here is more info on lodash if you need lodash
To run require successfully in React, this is what I did:
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); }
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.
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 ?
When you don't use a library, even you require it, it won't be required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.