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

Recommend

Several ways to connect tables in MySQL

The connection method in MySQL table is actually ...

Example of deploying Laravel application with Docker

The PHP base image used in this article is: php:7...

Detailed explanation of the text-fill-color property in CSS3

What does text-fill-color mean? Just from the lit...

Master the CSS property display:flow-root declaration in one article

byzhangxinxu from https://www.zhangxinxu.com/word...

How to solve the problem that MySQL cannot start because it cannot create PID

Problem Description The MySQL startup error messa...

Installation and configuration tutorial of MongoDB under Linux

MongoDB Installation Choose to install using Yum ...

Problems and solutions of using TweenMax animation library in angular

I have nothing to do recently, so I tinker with C...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

Solution to 1290 error when importing file data in mysql

Error scenario Use the mysql command in cmd to ad...

5 Commands to Use the Calculator in Linux Command Line

Hello everyone, I am Liang Xu. When using Linux, ...

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...