Detailed tutorial on React introducing container components and display components to Vue
不言
Release: 2018-05-05 11:48:07
Original
1721 people have browsed it
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!
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