今天肺炎增长数字依然吓人,感觉穿越到丧失片里了。

本节开始学习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;

注意:

红色部分如果写成

state.UserInfo.username=action.payload.UserInfo.username;
不会自动刷新
 
要写成
state=action.payload;
 
或者后两句写为
return { ...state,UserInfo:action.payload.UserInfo} 
 
因为必须返回一个新的引用才会更新页面

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实践的更多相关文章

  1. AntDesign(React)学习-10 Dva 与后台数据交互

    明天正式在线办公没时间学习了,今天晚上再更新一篇, 代码提交一次:https://github.com/zhaogaojian/jgdemo 1.src下创建services目录 创建文件userSr ...

  2. AntDesign(React)学习-1 创建环境

    目录: AntDesign(React)学习-15 组件定义.connect.interface AntDesign(React)学习-14 使用UMI提供的antd模板 AntDesign(Reac ...

  3. AntDesign(React)学习-3 React基础

    前面项目已经建起来了,但是没有React基础怎么办,从头学习,这个项目使用的是基于React16.X版本的几种技术集成,那么我们就从网上找一些相关的资料进行研究,我的习惯是用到哪学到哪. 一.先看一些 ...

  4. AntDesign(React)学习-14 使用UMI提供的antd模板

    1.UMI提供了可视化antd模板,可以直接添加到项目中修改用 比如将个人中心添加到项目中 2.选择个人中心,确定 3.成功 4.打开项目 5.Route文件也自动添加 根路由有exact:true后 ...

  5. AntDesign(React)学习-5 路由及使用Layout布局

    前言:学习目标实现点击登录按钮,直接进入后台布局页面,类似下面antd官网文档展示效果 ant.design访问 https://ant-design.gitee.io/components/menu ...

  6. AntDesign(React)学习-2 第一个页面

    1.前面创建了第一个项目jgdemo,结构如下,使用TypeScript. 2.yarn start启动项目 3.点击GettingStarted是umi的官方网站 https://umijs.org ...

  7. AntDesign(React)学习-15 组件定义、connect、interface

    虽然常用的编码用一种即可,但是看别人文档或者示例时,有的写法不熟悉的话看着很不习惯,整理几种实现同一功能的不同写法 1.Dva Connect与@Connect import React, { Pro ...

  8. 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 ...

  9. AntDesign(React)学习-12 使用Table

    AntDesign(Vue)版的Table中使用图片https://www.cnblogs.com/zhaogaojian/p/11119762.html 之前在使用VUE版Table时,使用大图片时 ...

随机推荐

  1. 邓士鹏【MySql大全】

    禁止使用系统关键字: typename 1.计算两个日期的时间差函数 SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是 ...

  2. UCF Local Contest 2015 J 最小割

    题意: 有

  3. Erlang/Elixir精选-第6期(20200113)

    精选文章 Implementing languages on the Erlang VM. -Robert Virding. 因为视频没有显示PPT,PPT可以在点击这里下载. leex - lexi ...

  4. WebStorm2018破解教程

    话不多说,直接上教程: 1,下载压缩包,并解压缩,下载地址如下: 链接:谁点谁知道提取码:9am8 2,双击压缩包中的WebStorm-2018.2.1.exe文件,进行安装. 3,安装完成之后,将压 ...

  5. VMvare桥接网络连接不上解决办法

    记一次学习中的突发状况.由于本人的pc时长要在不同的网络中进行切换,ip地址一般都是不固定的,所以我使用虚拟机的时候一般使用的都是让VMvare自动识别网络环境.直到今天遇到一种突发情况,VMvare ...

  6. vuex目录配置

    vuex目录配置,即vue-cli开发时目录配置 项目结构 Vuex 并不限制你的代码结构.但是,它规定了一些需要遵守的规则: 应用层级的状态应该集中到单个 store 对象中. 提交 mutatio ...

  7. ES6常用语法(二)

    arrow functions (箭头函数) 函数的快捷写法.不需要 function 关键字来创建函数,省略 return 关键字,继承当前上下文的 this 关键字 // ES5 var arr1 ...

  8. Windos framework .net 3.5规则失败

    1. 安装的时候有一个.net警告,这里给后边造成了一个隐患,实际上是wondows少了一个framework .net的插件,跟SQLserver安装本身没关系,一开始忽略了,后边就报错了. 2.点 ...

  9. opencv —— 调用摄像头采集图像 VideoCapture capture(0);

    如果要调用摄像头进行视频采集,将代码 VideoCapture capture("C:/Users/齐明洋/Desktop/1.mp4"); 中的 "C:/Users/齐 ...

  10. 我国自主开发的编程语言“木兰”居然是一个披着“洋”皮的Python!

    究竟是真“自主”,还是又一个披着“洋”皮的“红芯浏览器”? ​ 作者 | 沉迷单车的追风少年 出品 | CSDN博客 昨天看到新闻: ! ​ 心头一震,看起来很厉害啊!毕竟前几天美国宣布要对中国AI软 ...