Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native to use absolute paths? instead of doing import X from '../../something/X'?

I'm wondering the best way for me to use a full path for my project.

For example, if I am importing a component, I don't want to do import Component from '../components/Component', rather just do import Component from 'app/components/Component';

Can someone help me out?

like image 956
user3152131 Avatar asked May 29 '16 17:05

user3152131


People also ask

How do you get absolute path of a file in react native?

The most convenient way : Use react-native-document-picker, on selection it will give you a Relative path, something like this content://com.android...... . Pass that Relative path to Stat(filepath) function of the react-native-fetch-blob library. The object will return absolute path.

How do I enable absolute imports in react?

Right-click the src folder and select Mark Directory as and Click Resource Root . Next select Preferences -> Editor -> Code Style -> JavaScript -> Imports and Check Use paths relative to the project, resource or source roots and then Click Apply .


2 Answers

To have an absolute path,

I am considering your package.json is in the root.

Inside, your package.json add a new property name with value whatever you want i.e

"name": "root"

and then import all your routes relative to that route

import Component from root/components/Component
like image 65
iRohitBhatia Avatar answered Sep 29 '22 11:09

iRohitBhatia


You can use absolute path by figuring out the name of your app in the package.json name section and then using it in front of your absolute path i.e

import Component from MyApp/components/Component

Here's more information on describing the issue https://github.com/facebook/react-native/issues/3099

like image 36
Jhony Fung Avatar answered Sep 29 '22 10:09

Jhony Fung