[React Native] Reduce Long Import Statements in React Native with Absolute Imports

In large React Native projects, it’s common to have long relative import paths like:

import MyComponent from '../../../screens/MyScreen/MyComponent'

With import paths that go up and down the folder hierarchy like that, it can be confusing to figure out which file or folder you’re actually importing, and it generally just looks messy to have several of those import statements at the top of your file.

Instead, we can convert relative import paths to absolute import paths by creating a new package.json file at any level of the folder hierarchy. That package.json file needs to specify a name that you want to call that folder:

{ "name": "screens" }

And then you can begin your import statements with that new module name:

import MyComponent from 'screens/MyScreen/MyComponent'

Note that this only works for React Native projects, and not other npm based projects like create-react-app web apps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值