当前位置:网站首页>How to use the DVD entry level in taro3.*
How to use the DVD entry level in taro3.*
2022-06-29 21:48:00 【yunchong_ zhao】
If you use dva do react Project status management , It's not so cool , This is the latest To do a small program , Do not use native development , I chose taro frame , To develop applets ,
But I still want to use state management dva What shall I do? . In fact, it can still be used
Students, please continue to look down
Blogger's taro The version is 3.4 Of course It is relatively new
1, A good workman does his work well , You must sharpen your tools first Before the start , We installed a lot of things
yarn add react-redux redux dva-core dva-loading --save
Then start the configuration
stay src Create one under the file models Folder
Let me take a classic example here . Just explain the counter as the status
models/index.js models/count.js I created two files
models/index.js
import count from './count'
export default [count]
models/count.js Here are some examples Modify synchronously and asynchronously
export default {
namespace: "count",
state: {
count: 11112,
},
effects: {
* AsyncChangeCount(_, {
put }) {
const value = yield new Promise((resolve) => {
setTimeout(() => {
resolve(2)
}, 2000)
})
yield put({
type: "changeCount",
payload: {
count: value
}
})
}
},
reducers: {
setCount(state) {
state.count += 1;
return {
...state };
},
changeCount(state, {
payload }) {
state.count = payload.count
return {
...state }
}
},
};
Then is dva Tool files for
In your utils Under the document Create a dva.js In fact, it is used here The way other big guys write But let's say everything In fact, they are almost This is just a little encapsulated No big problem
import {
create } from 'dva-core'
import createLoading from 'dva-loading'
let app,store,dispatch;
function createApp (opt) {
// redux journal
// opt.onAction = [createLogger()];
app = create(opt)
app.use(createLoading({
}))
if (!global.registered) opt.models.forEach(model => app.model(model))
global.registered = true
app.start()
store = app._store
app.getStore = () => store
dispatch = store.dispatch
app.dispatch = dispatch
return app
}
export default {
createApp,
getDispatch () {
return app.dispatch
},
getState: () => store.getState()
}
The play is app.js Configuration in
import {
Component } from 'react'
import {
Provider } from "react-redux";
import dva from "./utils/dva"
import models from './models';
import './app.scss'
const dvaApp = dva.createApp({
initialState: {
},
models,
});
const store = dvaApp.getStore();
class App extends Component {
// this.props.children Is the page that will be rendered
render () {
return <Provider store={
store}>{
this.props.children }</Provider>
}
}
export default App
The above is that the configuration is completed . Then start using... In the component .
After all, now react yes hooks The era of the I also use hooks Let's take an example
The core is from react-redux There are two hooks Everything else adds up class Type usage is similar
import { useSelector, useDispatch } from ‘react-redux’
import {
View } from "@tarojs/components";
import {
useState } from "react";
import {
AtButton } from "taro-ui";
// Introduce on demand taro-ui
import "taro-ui/dist/style/components/button.scss";
import "taro-ui/dist/style/components/loading.scss";
import {
useSelector, useDispatch } from 'react-redux'
import './index.scss'
export default () => {
const [count, setCount] = useState(0);
const count1 = useSelector(state => state.count.count)
const dispatch = useDispatch();
return (
<View>
<View>component state: {
count }</View>
<View>redux data: {
count1 } </View>
<View className='h100'></View>
<AtButton type='primary' loading size='small' onClick={
() => setCount(count + 1)}>click change state </AtButton>
<View className='h100'></View>
<AtButton type='primary' loading size='small' onClick={
() => dispatch({
type: "count/setCount"})}>click change redux</AtButton>
<View className='h100'></View>
<AtButton type='primary' loading size='small' onClick={
() => dispatch({
type: "count/AsyncChangeCount"})}> Asynchronous modification redux The number </AtButton>
</View>
);
};
That's it . Continue to use dva stay taro I'm very happy .
Follow me to keep updating Front end knowledge .
边栏推荐
- If the evaluation conclusion of waiting insurance is poor, does it mean that waiting insurance has been done in vain?
- Viewing technological changes through Huawei Corps (V): smart Park
- About Effect Size
- THREEJS基础入门
- How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
- Vipshop Keyword Search API interface (item_search- search vipshop commodity API interface by keyword), vipshop API interface
- A. Marathon
- leetcode:307. 区域和检索 - 数组可修改
- Shutter bottomnavigationbar with page switching example
- Aleph farms hired a supervisor of regulatory affairs to prepare for global commercialization in advance
猜你喜欢

About Effect Size

leetcode:724. 寻找数组的中心下标

VHDL电话计费系统设计

leetcode:238. 除自身以外数组的乘积

Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech

Star ring technology data security management platform defender heavy release

Layer 3 loop brought by route Summary - solution experiment

A new Polaris has risen!

Bs-gx-017 online examination management system based on SSM

Summer Challenge harmonyos ark development framework arkui streamer button effect
随机推荐
How to prepare samples for application of color coated steel sealing plates to BS 476-3?
【ROS进阶篇】第二讲 自定义头、源文件封装
炒股开户请问哪个券商佣金是最低最安全的
[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)
C. Where‘s the Bishop?
Amazon Keyword Search API interface (item_search- Amazon product search interface by keyword), Amazon API interface
Star ring technology data security management platform defender heavy release
Basic qualities of management personnel
LeetCode 1. 两数之和
Implementing LDAP proxy service with haproxy + keepalive
GoAhead WebServer移植
小型图书馆项目总结
【ROS进阶篇】第四讲 ROS中的重名问题(节点、话题与参数)
数字密码锁verilog设计+仿真+上板验证
Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech
美国隧道法ASTM E84 表面阻燃测试
A new Polaris has risen!
leetcode:238. 除自身以外数组的乘积
THREEJS基础入门
Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle