Absrtact: It's a cool thing to be a top-level component that can use props to control and pass all States to its subcomponents.
Do not confuse with "MVC", we call it "view Controller" or "Controller View" only if we can control and pass all the top-level components of "state".
For any top-level component, such as
Example
//Controller views is very simpleclass Homepagecontroller extends React.component {//Normal Flux Store listeningComponentdidmount () {Store1.on (' Change ', This. Onstorechange); Store2.on (' Change ', This. Onstorechange); } onstorechange () { This. SetState ({data1:Store1.getData (), Data2:Store2.getData ()}); } render () {// return<Homepage data1={ This. state.data1} data2={ This. state.data2}/>; }}
The benefits of Controller views mode
Components are more convenient
Assuming you have a component called <ShoppingCartView/> and a component called <ShoppingCartStore/>, we need a component to listen directly to the state of the store component, which is obvious. But then again assuming that you have two copies of <ShoppingCartView/> to read the data from different Stroe at the same time, what to do, assuming you have a component called <shoppingcartcontroller/>, You can listen to more than one store and then use props to render each sub-component.
The following example
class Shoppingcartcontroller extends React.component { ... // You get component portability for free! render () { return <div> <shoppingcart data={store1data}/> < ShoppingCart Data={store2data}/> </div>; }
with React Router a seamless fit
React Router has a <routehandler/> that points to each page; If you have a view controller, you already have a route handler.
This is even more important if your component needs to read the parameters inside the URL.
For example Context.router.getCurrentQuery (), you use props to pass the URL's parameter rendering, rather than putting your rendering method inside the seven or eight bad. Now your subcomponents get the most introductory rendering with the help of props, not the subcomponents to get the parameters of the URL and then render it here.
You can also use willtransition as a hook for the controller view, which is used to make your subcomponents more convenient because they never have to worry about dealing directly with routing requests.
Reduce overhead
State control is not recommended in a computer system because a method is difficult to understand after a glance, and you need to go through the details to see the content.
If you nest components and each component handles their own state, your application will be difficult to understand.
But when a component only needs to be rendered purely on the basis of data obtained by props, we will make it easier to understand the output of this component. Because the build has this explicit input.
Easier to test
If you want to test a subassembly and just want to test the specific props of the component and the explicit rendering input results, you will not have to invent the test scenario and the request action.
Question
What happens if multiple "View Controllers" are nested?
Controller views are not always top-level components, such as
The answer is in the negative. If the Lightbox component has a display on multiple pages, it is a good idea to control lightbox by the
A sub-component can you have any internal status?
Yes, you can. In fact, in Controller view, the use of internal state is more pronounced. For example you have a component called <searchform/> then it can have a data store for your subcomponents called This.state.unsubmittedSearch,
Concern
The main problem with this controller view is the nested problem of the component. Controller view needs to be very small and simple, and if you have a deep-seated component nesting, your controller view will become very big and clumsy because you need to pass a lot of props and change a lot of states.
A solution like this particular scenario, but this is usually code intuition, you need to evaluate your component tree so that it makes sense for you to judge which components are nested.
For example, if you have a multi-step form with more than 20 pages, you need to ship a lot of props, but if you have a controller view for a specific URL at each step, your component will become very small and simple.
Controller view mode for Reactjs