This article mainly introduces the detailed tutorial of React introducing container components and display components into Vue. The article introduces the reasons for using container components in great detail. Friends who need it can refer to it
If you If you have used Redux to develop React, you must have heard of container components (Smart/Container Components) or presentation components (Dumb/Presentational Components). What are the benefits of this division? Can we learn from this division to write Vue code? ? This article will demonstrate why we should adopt this pattern and how to write both components in Vue.
Why use container components?
If we want to write a component to display comments, before we have heard of container components, our The code is generally written like this:
It seems natural to write this way. Are there any problems or areas that can be optimized?
There is an obvious problem. Since CommentList.vue is coupled with the Vuex store of the project, it is difficult to reuse it without the current project.
Is there a better way to organize components that can solve this problem? It’s time to understand the concept of container components in the React community.
What is a container component
In React.js Conf 2015, there is a topic of Making your app fast with high-performance components introduced Container component.
The container component is specifically responsible for communicating with the store and passing data to the ordinary display component through props. If the display component wants to initiate an update of data, it also passes it through the container component through props. The callback function tells the store.
Since the display component is no longer directly coupled with the store, but defines the data and methods it needs through the props interface, the reusability of the display component will be higher.
The difference between container components and presentation components
Display component
Container component
#Function
Describe how to display (skeleton, Style)
Describe how to run (data acquisition, status update)
connector 实际上是一个能获取到 store 实例的连接器,可以在初始化 vuex store 的时候进行初始化。
import Vue from 'vue';
import Vuex from 'vuex';
import VuexConnector from '@xunlei/vuex-connector';
Vue.use(Vuex);
const store = new Vuex.Store({
// your store
});
export const connector = new VuexConnector(store);
export default store;
VuexConnector 不依赖 this.$store,而是依赖初始化传入的 store 实例,容器组件可以用 connect 将展示组件与 store 进行连接。
由于不依赖 this.$store,我们在程序入口 new Vue 的时候,就不需要传入 store 实例了。
比如,之前我们是通过下面的方式进行初始化:
import Vue from 'vue';
import App from './App';
import store from './store';
new Vue({
el: '#app',
components: {App},
template: '<App/>',
store,
});
Copy after login
使用了 VuexConnector 之后,在最初 new Vue 的时候就不需要也最好不要传递 store 了,这样就避免了 this.$store 泛滥导致代码耦合的问题。
引入容器组件/展示组件模式带来的好处
可复用性
容器组件/展示组件的划分,采用了单一职责原则的设计模式,容器组件专门负责和 store 通信,展示组件只负责展示,解除了组件的耦合,可以带来更好的可复用性。
The above is the detailed content of Detailed tutorial on React introducing container components and display components to Vue. 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
Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box
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.
The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.
Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open
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.
Vue component development: Progress bar component implementation method Preface: In Web development, the progress bar is a common UI component, often used to display the progress of operations in scenarios such as data requests, file uploads, and form submissions. In Vue.js, we can easily implement a progress bar component by customizing components. This article will introduce an implementation method and provide specific code examples. I hope it will be helpful to Vue.js beginners. Component structure and style First, we need to define the basic structure and style of the progress bar component.
Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment
Vue component development: drop-down menu component implementation method In Vue.js, the drop-down menu is a common UI component used to display a set of options for users to select. This article will introduce how to use Vue.js to develop a drop-down menu component and provide specific code examples. Create a Vue component First, we need to create a Vue component to represent the drop-down menu. Register this component in the components option of the Vue instance so that it can be used in other components. //DropdownMenu.