dvadva is a data flow solution based on redux and redux-saga. In order to simplify the development experience, dva also has built-in react-router and fetch, which can be transformed into a lightweight application framework. Using dva Define a model, a bit like redux-toolkit. Connect like redux's connect. start up. Find the corresponding reducer through the corresponding instructions and dispatch it. This is the basic operation of dva, which is relatively easy to use. Implementing DVAIdea: Encapsulate a function, create a store, collect reducers and states through models, and change the method name in reducers. First, the model. Because there are multiple models, we need to collect them. First, change the method name in rducer through namesapce. Then save it. Then it can be injected through Provider. Transform the reducer of the model and return the reducer accepted by redux. At this time, the state of the model will not change. It is just the initial state. What the component actually accepts is the state in redux. Initial implementation completed. Support asyncDva is integrated with redux-saga, so its usage is similar. When creating a model, you can create a saga middleware through effects. Then dispatch the corresponding action accomplish Idea: process the effects of each model, convert them into corresponding saga functions, use takeEvery to listen to each attribute name (such as asyncADD, converted to takeEvery('asyncADD', ()=>{}), etc.), then create a middleware through createSagaMiddleware provided by redux-saga, and use the run method to execute the saga functions converted from the effects in each model. Use getSagas to transform the effects of each model and store them in an array, then run them one by one through forEach. It can be seen that the effects of each model will be converted into function*(){} in getSaga, and then the effects of a single model will be traversed, and each attribute will be processed by *asyncADD(){}, as shown in the figure. A saga function is returned through getWatcher, and a child process is started through fork, which will not interfere with the operation of the main process. Each getWatcher returns a saga function (generator function), which monitors the current attribute (key) through takeEvery and executes the subsequent effects. Here we need to modify the Put method of redux-saga to make it do some prefixType logic separately. That's how it's done. It can run normally. Implementation of router There is no difference in using So the usage is similar to it. Note that asynchronous logic can only be written in effects. Reducer is synchronous and cannot continue to dispatch. accomplish:Based on connectd-react-router Remember how to use connectd-react-router? The first step is to create a router object. The second step is to use middleware. Pass the api to app._router as a parameter. That's it. When he sends a push, it will be intercepted and processed by the routing middleware and pushed to another page. This is the end of this article about react dva implementation. For more relevant react dva implementation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! |
>>: Meta viewport makes the web page full screen display control on iPhone
This article example shares the specific code for...
This article shares with you how to use Vue to lo...
1. Pull the image docker pull registry.cn-hangzho...
After the release of CentOS8.0-1905, we tried to ...
1. As mentioned above I saw this macro when I was...
In MySQL, how do you view the permissions a user ...
Three functions: 1. Automatic vertical centering o...
Format Encoding 1. Please set the page width with...
Check whether your cuda is installed Type in the ...
1. Basic Concepts 1. Sitemesh is a page decoratio...
Recently I need to change the version of MySQL da...
I will be learning MySQL next semester. I didn...
This article mainly introduces the case of Vue en...
Problem background: There is a requirement to tak...
1. Create a table CREATE TABLE `student` ( `id` i...