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

简化React Native导入路径
本文介绍了一种在React Native项目中使用绝对导入路径的方法来替代冗长的相对导入路径,以此提高代码的可读性和整洁度。通过创建特定的package.json文件并指定名称,可以实现从任意层级开始的简洁导入。

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.

转载于:https://www.cnblogs.com/Answer1215/p/8931064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值