当前位置:网站首页>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 .
边栏推荐
- Dynamic planning idea "from getting started to giving up"
- The MySQL database in Alibaba cloud was attacked, and finally the data was found
- 身体质量指数程序,入门写死的小程序项目
- Let's talk about 15 data source websites I often use
- "Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
- MySQL中回表的代价
- Realize incremental data synchronization between MySQL and ES
- golang中的atomic,以及CAS操作
- Let's see through the network i/o model from beginning to end
- UI control telerik UI for WinForms new theme - vs2022 heuristic theme
猜你喜欢

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

第三方跳转网站 出现 405 Method Not Allowed

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform

go-zero微服务实战系列(九、极致优化秒杀性能)

2022 Google CTF SEGFAULT LABYRINTH wp

《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03

Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version

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

让我们,从头到尾,通透网络I/O模型
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
随机推荐
Segmenttree
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
自旋与sleep的区别
NEON优化:性能优化常见问题QA
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
ESP Arduino (IV) PWM waveform control output
接收用户输入,身高BMI体重指数检测小业务入门案例
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
阿里云中mysql数据库被攻击了,最终数据找回来了
NEON优化:矩阵转置的指令优化案例
Lldp compatible CDP function configuration
go-zero微服务实战系列(九、极致优化秒杀性能)
Dell筆記本周期性閃屏故障
Installation of torch and torch vision in pytorch
JTAG principle of arm bare board debugging
paddlehub应用出现paddle包报错的问题
Oracle: Practice of CDB restricting PDB resources
There is an error in the paddehub application
Windows installation mysql8 (5 minutes)