当前位置:网站首页>taro3.*中使用 dva 入门级别的哦
taro3.*中使用 dva 入门级别的哦
2022-07-06 17:29:00 【yunchong_zhao】
如果你用dva做react项目的状态管理,简直不要太爽了,这部最近 要做一个小程序, 不让用原生开发, 我就选择了taro框架,来开发小程序,
但是状态管理这块我还想用dva 怎么办呢。其实还是可以用的
各位同学请接着往下看
博主的taro版本是 3.4的哦 还是比较新的呢
1,工欲善其事,必先利其器 开始之前,我们安装一大堆东西哈
yarn add react-redux redux dva-core dva-loading --save
然后就开启配置下了
在src文件下创建一个 models的文件夹
我这里还是拿个经典的例子。就那个计数器作为状态的讲解哈
models/index.js models/count.js 我创建了两个文件
models/index.js
import count from './count'
export default [count]
models/count.js 我这里分别举例了 同步和异步的方式修改
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 }
}
},
};
然后就是 dva的工具文件了
在你的utils文件下 创建一个 dva.js 其实这里用了 别的大佬的写的方法 但是一通百通吧 其实都差不多 这个只是稍微有封装了下 问题不大
import {
create } from 'dva-core'
import createLoading from 'dva-loading'
let app,store,dispatch;
function createApp (opt) {
// redux日志
// 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()
}
重头戏就是app.js中的配置了
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 是将要会渲染的页面
render () {
return <Provider store={
store}>{
this.props.children }</Provider>
}
}
export default App
上面的就是配置完成了。然后开始在组件中使用。
毕竟现在react是hooks的时代了 我也用hooks来举例子吧
核心就是从react-redux中引出两个hooks 其他的都和class类型用法差不多
import { useSelector, useDispatch } from ‘react-redux’
import {
View } from "@tarojs/components";
import {
useState } from "react";
import {
AtButton } from "taro-ui";
// 按需引入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"})}>异步修改redux数值</AtButton>
</View>
);
};
这下就可以了。继续可以使用dva在taro中很开心。
关注我持续更新 前端知识。
边栏推荐
- [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
- boot - prometheus-push gateway 使用
- Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
- Dell笔记本周期性闪屏故障
- 【JVM调优实战100例】04——方法区调优实战(上)
- STM32开发资料链接分享
- golang中的WaitGroup实现原理
- 界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
- ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
- 身体质量指数程序,入门写死的小程序项目
猜你喜欢

【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析

Dr selection of OSPF configuration for Huawei devices

"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!

Part VI, STM32 pulse width modulation (PWM) programming

Telerik UI 2022 R2 SP1 Retail-Not Crack

Periodic flash screen failure of Dell notebook

Windows installation mysql8 (5 minutes)

golang中的Mutex原理解析

省市区三级坐标边界数据csv转JSON
![[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)](/img/7a/bd03943c39d3f731afb51fe2e0f898.png)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
随机推荐
tensorflow 1.14指定gpu运行设置
阿里云中mysql数据库被攻击了,最终数据找回来了
Windows installation mysql8 (5 minutes)
第四篇,STM32中断控制编程
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
【JVM调优实战100例】04——方法区调优实战(上)
Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
重上吹麻滩——段芝堂创始人翟立冬游记
Niuke cold training camp 6B (Freund has no green name level)
Atomic in golang and CAS operations
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
NEON优化:log10函数的优化案例
Force buckle 1037 Effective boomerang
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
boot - prometheus-push gateway 使用
【JVM调优实战100例】05——方法区调优实战(下)
深入探索编译插桩技术(四、ASM 探秘)
NEON优化:关于交叉存取与反向交叉存取
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
Address information parsing in one line of code