React dva implementation code

React dva implementation code

dva

dva 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.

insert image description here

Using dva

insert image description here

Define a model, a bit like redux-toolkit.
Use connect

insert image description here
insert image description here

Connect like redux's connect.

insert image description here

start up.
Then how to dispatch actions.

insert image description here

Find the corresponding reducer through the corresponding instructions and dispatch it.

insert image description here

This is the basic operation of dva, which is relatively easy to use.

Implementing DVA

Idea: Encapsulate a function, create a store, collect reducers and states through models, and change the method name in reducers.

insert image description here

First, the model. Because there are multiple models, we need to collect them.

insert image description here

First, change the method name in rducer through namesapce.

insert image description here

Then save it.

insert image description here
insert image description here

Then it can be injected through Provider.

insert image description here

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.
This getReducer will only be called once to return the real reducer.

insert image description here

Initial implementation completed.

Support async

Dva is integrated with redux-saga, so its usage is similar.

insert image description here

When creating a model, you can create a saga middleware through effects. Then dispatch the corresponding action

insert image description here
insert image description here

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.
Code:

insert image description here

Use getSagas to transform the effects of each model and store them in an array, then run them one by one through forEach.
See how to convert

insert image description here

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.

insert image description here

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.
This is basically done. The effects of each model will be processed by the saga function to run, and each method of the effects is converted into a key and listened to through takeEvery. Here you also need to add a prefix to each method name of the effect

insert image description here

That's how it's done.

insert image description here

It can run normally.

Implementation of router

insert image description here
insert image description here

There is no difference in using
dva inherits connected-react-router

insert image description here
insert image description here

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.

insert image description here
insert image description here
insert image description here

accomplish:

insert image description here
insert image description here
insert image description here

Based on connectd-react-router

insert image description here

Remember how to use connectd-react-router? The first step is to create a router object.

insert image description here

The second step is to use middleware.

insert image description here

Pass the api to app._router as a parameter. That's it.

insert image description here

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!

<<:  CSS3 frosted glass effect

>>:  Meta viewport makes the web page full screen display control on iPhone

Blog    

Recommend

Mini Program to Implement Slider Effect

This article example shares the specific code for...

Detailed explanation of how to use Vue to load weather components

This article shares with you how to use Vue to lo...

Detailed explanation of the process of building an MQTT server using Docker

1. Pull the image docker pull registry.cn-hangzho...

How to install and configure ftp server in CentOS8.0

After the release of CentOS8.0-1905, we tried to ...

In-depth analysis of the Linux kernel macro container_of

1. As mentioned above I saw this macro when I was...

Win10 uses Tsinghua source to quickly install pytorch-GPU version (recommended)

Check whether your cuda is installed Type in the ...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

MySQL 8.0.19 installation and configuration tutorial under Windows 10

I will be learning MySQL next semester. I didn...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...