当前位置:网站首页>Using the entry level of DVA in taro3.*
Using the entry level of DVA in taro3.*
2022-07-07 01:17: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 .
边栏推荐
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- golang中的WaitGroup实现原理
- Dell Notebook Periodic Flash Screen Fault
- Gnet: notes on the use of a lightweight and high-performance go network framework
- Telerik UI 2022 R2 SP1 Retail-Not Crack
- paddlehub应用出现paddle包报错的问题
- Realize incremental data synchronization between MySQL and ES
- Segmenttree
- 2022 Google CTF SEGFAULT LABYRINTH wp
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
猜你喜欢

Dynamic planning idea "from getting started to giving up"

Do you understand this patch of the interface control devaxpress WinForms skin editor?

Wood extraction in Halcon

Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
![[Niuke] [noip2015] jumping stone](/img/9f/b48f3c504e511e79935a481b15045e.png)
[Niuke] [noip2015] jumping stone
![[牛客] B-完全平方数](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[牛客] B-完全平方数

ARM裸板调试之JTAG原理

身体质量指数程序,入门写死的小程序项目

Gazebo的安装&与ROS的连接

JTAG principle of arm bare board debugging
随机推荐
实现mysql与ES的增量数据同步
Gazebo的安装&与ROS的连接
MySQL中回表的代价
Boot - Prometheus push gateway use
微信公众号发送模板消息
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
「笔记」折半搜索(Meet in the Middle)
Installation of torch and torch vision in pytorch
NEON优化:log10函数的优化案例
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
Rainstorm effect in levels - ue5
Segmenttree
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
table表格设置圆角
Realize incremental data synchronization between MySQL and ES
如何管理分布式团队?
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
tensorflow 1.14指定gpu运行设置
Atomic in golang, and cas Operations
第三方跳转网站 出现 405 Method Not Allowed