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

Let's talk about the two functions of try catch in Javascript

The program is executed sequentially from top to ...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Research on the effect of page sidebar realized by JS

Table of contents Discover: Application of displa...

Example code for implementing card waterfall layout with css3 column

This article introduces the sample code of CSS3 c...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...

Implementation of socket options in Linux network programming

Socket option function Function: Methods used to ...

Solve the problem of managing containers with Docker Compose

In Docker's design, a container runs only one...

JavaScript data visualization: ECharts map making

Table of contents Overview Precautions 1. Usage 2...

PHP related paths and modification methods in Ubuntu environment

PHP related paths in Ubuntu environment PHP path ...

js implements table drag options

This article example shares the specific code of ...

A detailed discussion of evaluation strategies in JavaScript

Table of contents A chestnut to cover it Paramete...