zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    UGO小组冲刺第一天
    day04_07-三个函数的区别
    day06_08 字符串
    day06_07 字典操作02
    day06_06 字典操作01
    day06_05 字典
    day06_04 购物车讲解02
    day06_03 购物车讲解01
    day06_02 元组
    day06_01 上节回顾
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8931064.html
Copyright © 2011-2022 走看看