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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.

以上是关于[React Native] Reduce Long Import Statements in React Native with Absolute Imports的主要内容,如果未能解决你的问题,请参考以下文章

我如何从 JSON React Native 中获得价值?

Type mismatch in key from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.Lon

前端学习(3171):react-hello-react之reduce

React Redux:在 Reduce 状态下使用不可变

reduce ,redceRight 在react中的使用

react-native init 指定 react 版本和 react-native 版本