24
loading...
This website collects cookies to deliver better user experience
.js
and .jsx
files import many libraries, components, pages, controllers, services, helpers, styles, that is, a multitude of different file types and data.import React, { useState, useEffect } from 'react';
import Button from '~/components/Button';
import { Container } from './styles.js';
import Card from '~/components/Card';
import PropTypes from 'prop-types';
import { combineReducers } from 'redux';
import Main from '~/pages/Main';
import Dashboard from '~/components/Dashboard';
import Home from '~/pages/Home';
import Routes from '~/routes';
function ExampleComponent() { ...
But if we can make it more organized, why not?
import React, { useState, useEffect } from 'react';
import { combineReducers } from 'redux';
import PropTypes from 'prop-types';
import Home from '~/pages/Home';
import Main from '~/pages/Main';
import Button from '~/components/Button';
import Card from '~/components/Card';
import Dashboard from '~/components/Dashboard';
import Routes from '~/routes';
import { Container } from './styles.js';
function ExampleComponent() { ...
first: all the dependencies that I consider the most important of my application. For example, all those starting with 'react' and 'redux'
then: all other libraries and modules installed in package.json. For example, Prop Types, Storybook, Unform, among others
then: all internal files, which start with the alias ~/
or @
. For example, ~/components
, ~/pages
, ~/styles
, @controllers
, @models
, and so on. In the post Import custom paths and how this can help you I show you how we can create and configure custom import paths to facilitate the import of our application
so: the routes of my application"
and lastly: my styles. In this case, files named .styled.js
// first, everything that starts with 'react' and 'redux'
import React, { useState, useEffect } from 'react';
import { combineReducers } from 'redux';
// then all imported modules and libraries
import PropTypes from 'prop-types';
// then anything that starts with an alias '~/pages' or '@pages'
import Home from '~/pages/Home';
import Main from '~/pages/Main';
// then anything that starts with an alias '~/components' or '@components'
import Button from '~/components/Button';
import Card from '~/components/Card';
import Dashboard from '~/components/Dashboard';
// so my routes
import Routes from '~/routes';
// and finally, my styles
import { Container } from './styles.js';
function ExampleComponent() { ...
.eslintrc.js
configuration file.yarn add -D eslint-plugin-import-helpers
.eslintrc.js
file:// .eslintrc.js
rules: {
'import-helpers/order-imports': [
'warn', // displays an alert in the editor
{
newlinesBetween: 'always', // inserts a blank line after each priority group
groups: [
['/^react/', '/^redux/'], // first group: everything that starts with 'react' and 'redux'
'/styled-components/', // second group: everything that is imported directly from the styled-components library. This is to ensure that the first import in styles.js files is always styled-components.
'module', // third group: everything that is a module (any library listed in the package.json file)
'/prop-types/', // fourth group: importing prop-types
'/^~/pages/', // fifth group: all my pages
'/^~/components/', // sixth group: all my components
['parent', 'sibling', 'index'], // seventh group: any parent, sibling, or child file of the current file
'/routes/', // eighth group: my routes
'/^~/', // ninth group: all other files imported by the configured alias
'/styles/', // last group: my styles
],
alphabetize: { order: 'asc', ignoreCase: true }, // configures imports in alphabetical (ascending) order, ignoring case
},
],
}