Home Web Front-end JS Tutorial Detailed explanation of React's use of referencing component instances relative to the root directory

Detailed explanation of React's use of referencing component instances relative to the root directory

Jan 27, 2018 am 11:19 AM
react Table of contents

This article mainly introduces to you the relevant information on how to use React to reference components relative to the root directory. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let's learn with the editor below, I hope it can help everyone.

In the components developed by myself, I often make references such as the following:


import genFetchEntryListArgs from '../../../utils/table/genFetchEntryListArgs';
import { parseQuery, stringifyQuery } from '../../../utils/query';
import mapMyToProps from '../../../utils/connect/mapMyToProps';
import genPagination from '../../../utils/table/pagination';
import handleConfirm from '../../../utils/handleConfirm';
import getBaseQuery from '../../../utils/getBaseQuery';
import setSortQuery from '../../../utils/setSortQuery';
import handleError from '../../../utils/handleError';
import injectProto from '../../../utils/injectProto';
import injectApi from '../../../utils/injectApi';
import querySchema from './querySchema';
import genColumns from './genColumns';
Copy after login

Although using relative path references in this way is It is a relatively common approach, but in medium and large projects, when a lot of components are introduced, it is extremely painful to write.

Of course, we can configure certain file directories as fixed imports by using resolve.alias in webpack to configure aliases.

For example, in the above example, we can set the utils folder as a utils alias. In the future, we can only import utils without writing a bunch of ../../../.

The configuration settings are as follows:


const path = require('path');

module.exports = {
 ...
 resolve: {
  alias: {
   'utils': path.resolve(__dirname, '../src/utils'),
  }
 },
 ...
};
Copy after login

After the top example has been rewritten, it should look like this:


import genFetchEntryListArgs from '../../../utils/table/genFetchEntryListArgs';
import { parseQuery, stringifyQuery } from 'utils/query';
import mapMyToProps from 'utils/connect/mapMyToProps';
import genPagination from 'utils/table/pagination';
import handleConfirm from 'utils/handleConfirm';
import getBaseQuery from 'utils/getBaseQuery';
import setSortQuery from 'utils/setSortQuery';
import handleError from 'utils/handleError';
import injectProto from 'utils/injectProto';
import injectApi from 'utils/injectApi';
import querySchema from './querySchema';
import genColumns from './genColumns';
Copy after login

Related recommendations:

php Several methods to obtain the physical path of the website root directory (recommended) Tips

The above is the detailed content of Detailed explanation of React's use of referencing component instances relative to the root directory. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

How to read the catalog when reading on WeChat How to view the catalog How to read the catalog when reading on WeChat How to view the catalog Mar 30, 2024 pm 05:56 PM

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

Use tree to generate a file directory tree for display Use tree to generate a file directory tree for display Mar 01, 2024 pm 05:46 PM

tree is a command line tool that recursively lists the contents of a directory in a tree format, so that all directories, subdirectories, and files are listed in a hierarchical manner, thereby visually displaying the organizational structure of files and folders. The following are the installation and use methods of tree under Windows and Linux systems. The installation and use of tree under Linux. Installing tree under Linux: aptupdate&&aptinstalltree The following are the common ways of using the tree command. #Display the directory tree under the specified path tree/d/temp #Limit the maximum display depth tree-L3 #Display only directories but not files tree-d #Display including hidden files and directories tr

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How to view the table of contents of Tomato Novel How to view the table of contents of Tomato Novel How to view the table of contents of Tomato Novel How to view the table of contents of Tomato Novel Mar 27, 2024 pm 11:40 PM

Tomato Novel is a super easy-to-use novel reading software. We came across a novel that particularly interested us in Tomato Novel. After reading it for a while, we found that the content was very exciting. We wanted to read the chapters that we haven’t seen yet. How to read the table of contents? Many friends don’t know how to read the chapter table of contents. Therefore, the editor will briefly understand it and finally compiled a guide on how to read the chapter table of contents. Interested friends can go directly to Come and take a look at how to view the chapter table of contents on this website. How to view the catalog of Tomato novels 1. First, you need to open the software, then find a novel on the homepage, click to enter the novel viewing interface; in the novel interface, click in the evaluation middle to call up the novel function interface, in the novel function in the interface

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

See all articles