AntDesign(React)学习-9 Dva model reducer实践
今天肺炎增长数字依然吓人,感觉穿越到丧失片里了。
本节开始学习dva model使用,官网的讲解太文档化,对新手实践不太友好,自己简化了一个最简单的演示代码。
1、在src,models文件夹下创建user.ts,初始化username为张三1
const UserModel = {
namespace: 'User',
state:{
UserInfo:{
username:"张三1"
}
},
reducers:{
updateUserState(state,action)
{
let currentUser=state.UserInfo;
console.log("CurrentUser:"+currentUser);
console.log(currentUser);
console.log("CurrentUserPayload:")
console.log(action.payload.UserInfo);
state=action.payload;
return state;
}
}
};
export default UserModel;
注意:
红色部分如果写成
2、修改user.tsx如下
import React from 'react';
import { Button,Input} from 'antd';
import {connect} from 'dva'; class User extends React.Component {
render() {
console.log("render:");
console.log(this.props);
return (
<div>用户管理
<div>姓名:{this.props.UserInfo.username}</div>
<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleClick}>修改</Button>
</div>
);
}
handleClick = e => {
const userInfo={
UserInfo:{
username:"张三2"
}
}
this.props.dispatch({
type:"User/updateUserState",
payload:userInfo
})
console.log('click: ', e);
};
}
const getUserInfoFromState=(state)=>{
console.log(state);
return {UserInfo:state.User.UserInfo} }
export default connect(getUserInfoFromState)(User) ;
3、运行效果如下
4、点击修改后
5、console日志如下
AntDesign(React)学习-9 Dva model reducer实践的更多相关文章
- AntDesign(React)学习-10 Dva 与后台数据交互
明天正式在线办公没时间学习了,今天晚上再更新一篇, 代码提交一次:https://github.com/zhaogaojian/jgdemo 1.src下创建services目录 创建文件userSr ...
- AntDesign(React)学习-1 创建环境
目录: AntDesign(React)学习-15 组件定义.connect.interface AntDesign(React)学习-14 使用UMI提供的antd模板 AntDesign(Reac ...
- AntDesign(React)学习-3 React基础
前面项目已经建起来了,但是没有React基础怎么办,从头学习,这个项目使用的是基于React16.X版本的几种技术集成,那么我们就从网上找一些相关的资料进行研究,我的习惯是用到哪学到哪. 一.先看一些 ...
- AntDesign(React)学习-14 使用UMI提供的antd模板
1.UMI提供了可视化antd模板,可以直接添加到项目中修改用 比如将个人中心添加到项目中 2.选择个人中心,确定 3.成功 4.打开项目 5.Route文件也自动添加 根路由有exact:true后 ...
- AntDesign(React)学习-5 路由及使用Layout布局
前言:学习目标实现点击登录按钮,直接进入后台布局页面,类似下面antd官网文档展示效果 ant.design访问 https://ant-design.gitee.io/components/menu ...
- AntDesign(React)学习-2 第一个页面
1.前面创建了第一个项目jgdemo,结构如下,使用TypeScript. 2.yarn start启动项目 3.点击GettingStarted是umi的官方网站 https://umijs.org ...
- AntDesign(React)学习-15 组件定义、connect、interface
虽然常用的编码用一种即可,但是看别人文档或者示例时,有的写法不熟悉的话看着很不习惯,整理几种实现同一功能的不同写法 1.Dva Connect与@Connect import React, { Pro ...
- AntDesign(React)学习-13 Warning XX should not be prefixed with namespace XXX
有篇UMI入门简易教程可以看看:https://www.yuque.com/umijs/umi/hello 程序在点击操作时报了一个Warning: [sagaEffects.put] User/up ...
- AntDesign(React)学习-12 使用Table
AntDesign(Vue)版的Table中使用图片https://www.cnblogs.com/zhaogaojian/p/11119762.html 之前在使用VUE版Table时,使用大图片时 ...
随机推荐
- 升级了NinjaLoveFish Excel量化表格
为了方便查看均价和止盈值,新建了两列 这样做的好处就是,针对一个股票,可以实现不同的多个网格布局,然后分别实现各自的盈利设定. 例如这是网格1 那么同时也可以存在网格2 就可以实现多个网格布局到一个股 ...
- PHP0026:PHP 博客项目开发3
- CF547E Mike and Friends [AC自动机,离线树状数组]
#include <cstdio> #include <queue> #include <vector> #define pb emplace_back using ...
- LAN、WAN和WLAN的区别
1.LAN 局域网(Local Area Network)接口,通俗讲就是路由和用户之间网线口: 2.WAN 广域网(Wide Area Network),通俗讲就是和猫外部网连接的网线口: 3.WL ...
- AOV拓扑排序实验总结-1
AOV拓扑排序实验总结-1 实验数据:1.实验输入数据在input.txt文件中2.对于n是指有顶点n个,数据的结束标志是一行0 0. 实验目的:获取优秀的AOV排序算法模板 数据结构安排 ...
- 双向链表的简单Java实现-sunziren
写在前面,csdn的那篇同名博客就是我写的,我把它现在在这边重新发布,因为我实在不想用csdn了,那边的广告太多了,还有就是那个恶心人的“阅读更多”按钮,惹不起我躲得起. 在上次分享完单向链表的简单编 ...
- Android电源管理基础知识整理
前言 待机.睡眠与休眠的区别? Android开发者官网当中提到"idle states",该如何理解,这个状态会对设备及我们的程序造成何种影响? 进入Doze模式中的idle状态 ...
- Pair类模板
>Pair的实现是一个结构体而不是一个类< 1.标准头文件 #include<utility> 似乎无需引入该文件,在std命名空间内也有pair类型 2.格式为:templa ...
- MAT(memory anlayzer tool)使用方法
Analyzing and understanding the memory use of an application is challenging. A subtle logic error ca ...
- NumPy迭代数组
numpy.nditer是NumPy的一个迭代器对象,提供能够灵活的访问一个或者多个属猪元素的方式. # 迭代 z=np.arange(6).reshape(3,2) for x in np.ndit ...