码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-24 17:35:09      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:hat   with   ati   reac   hierarchy   name   his   folder   which   

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.

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

标签:hat   with   ati   reac   hierarchy   name   his   folder   which   

原文地址:https://www.cnblogs.com/Answer1215/p/8931064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!