当前位置:网站首页>taro3.*中使用 dva 入门级别的哦
taro3.*中使用 dva 入门级别的哦
2022-06-29 15:24: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中很开心。
关注我持续更新 前端知识。
边栏推荐
- PWM to 0-5v/0-10v/1-5v linear signal transmitter
- Paging SQL (rownum, row_number, deny_rank, rank)
- Flink SQL task taskmanager memory settings
- JS will have variable promotion and function promotion
- 明德扬XILINX-K7-325T/410T核心板数据手册
- 遥感典型任务分析
- stlink故障修复
- 服务器的数据库连不上了【服务已起、防火墙已关、端口已开、netlent 端口不通】
- 12.UDP协议-bite
- Is it reliable to invest in REITs funds? Is REITs funds safe
猜你喜欢

I am 35 years old. Can I change my career to be a programmer?

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

Google 软件版本经历周期

在shop工程中,实现一个菜单(增删改查)

Hi,你有一份Code Review攻略待查收

Lumiprobe reactive dye miscellaneous dye: BDP FL ceramide

Sofaregistry source code | data synchronization module analysis

Lumiprobe reactive dye - amino dye: cyanine 5 amine

Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier

Unity C basic review 28 - delegation with return (p449)
随机推荐
el-table-column行按钮防重控制loading
File常用工具类, 流相关运用 (记录)
Implementing redis distributed locks using custom annotations
13.TCP-bite
雷达基本组成
TDesign, which gave us benefits last time, will tell us its open source story today
MCS: discrete random variable - binomial distribution
Sofaregistry source code | data synchronization module analysis
Lumiprobe reactive dye carboxylic acid: sulfo cyanine7.5 carboxylic acid
Why MySQL chooses b+ tree to store indexes
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
Real software testers = "half product + Half development"?
Unity C basic review 27 - delegation example (p448)
.NET程序配置文件操作(ini,cfg,config)
I am 35 years old. Can I change my career to be a programmer?
动作捕捉系统用于苹果采摘机器人
11.应用层数据传输格式/端口号-bite
Unity C basic review 28 - delegation with return (p449)
Lumiprobe 活性染料丨杂染料:BDP FL 神经酰胺
关于SQL+NoSQL : NewSQL数据库