Reputation: 41
I want to get current javascript script's dirname name, such as __dirname
in node.js.
I haven't found the method in the document in react native , and I wonder how to achieve this purpose?
Or how can I locate the static file (e.g. .js)? For example like using NSDocumentDirectory
to locate a file natively in iOS.
Upvotes: 3
Views: 4729
Reputation: 16477
One way to do it would be to create a native module. Here's an example of a really simple one that just gets you a string, which you could customise for your exact requirements. In Xcode, create a new class:
// DirName.h
#import "RCTBridgeModule.h"
@interface DirName : NSObject<RCTBridgeModule>
@end
// DirName.m
#import "DirName.h"
@implementation DirName
- (void) get:(RCTResponseSenderBlock)callback {
RCT_EXPORT();
// Change this depending on what you want to retrieve:
NSString* dirName = @"something";
callback(@[dirName]);
}
@end
Then call it with:
var dirName = require('NativeModules').DirName;
dirName.get(dirName => {
console.log(dirName);
});
However I'd caveat this. Have a think about what you're doing - do you definitely want to read the filesystem of your iOS device or do you want to use one of the other methods of grabbing documents which is available to you?
Upvotes: 3