项目体系说明:react+mobx+antd

11. state设置对象属性

 this.setState({
tableParams:{tableName:value}
})

10.loading组件设置

this.setState({
title: Utils.trim(query.title),
loading:true,
}); this.props.articleService.findPage(query, true)
.then(data => this.setState({loading: false}))
.catch(e => this.setState({loading: false}));

9.模态框弹出时,需要传递参数应该怎么设置?

showModal = (obj,b) => {

    this.setState({
visible: true,
oldTopicId: obj.id,
});
console.log("a==============" ,obj); console.log("b==============" ,b);
// 获取主题词下所有关键词的id集合
const myParams = {};
myParams.topicId = obj.id;
this.topicState.getKeyWordIds(myParams);
}; //---------------------------------------------------------------------------
const columns = [{
title: '主题',
dataIndex: 'name',
key: 'name',
}, {
title: '关键词',
dataIndex: 'keywords',
key: 'keywords',
render: (item) => item ? item.join(',') : ''
}, {
title: '操作',
key: 'operation',
render: (_, record) => (
<div>
<Link to={`/topic/${record.id}`}><Icon type="edit"/>编辑</Link>
<span className="ant-divider"/>
<a className="fgw-text-error" onClick={this.showModal.bind(this, record)}><Icon type="delete"/>删除</a> </div>)
}];

8.当切换路由后(切换了左侧导航栏后),用户在返回之前查询的结果界面,结果界面还会展示上一次的结果,应该如何处理呢?

//第一步
componentWillUnmount() {

  this.props.store.docClassifyState.clearClassifyResult();
}

//第二步,在对应的state里面写一个清空数据源的函数 /* * 清空已有的数据源 * */
clearClassifyResult(){
this.setClassifyResult({});
}

7.Input.Search组件下,如何配合分页组件传递搜索框中的参数

constructor(props) {
super(props);
this.state = {
params: '',
};
this.store = this.props.store.websiteState;
} const paginationProps = {
pageSize: size || 0,
currentPage: number || 0,
totalElements: totalElements || 0,
showSizeChanger: true,
onChange: (page, size) => {
this.store.getWebsitePage({page, size, name: this.state.params});
}
}; <Input.Search placeholder="输入网站名称或网址进行搜索"
onSearch={value =>{
if (value === this.state.params) {
return
}
this.setState({params: value});
this.store.getWebsitePage({name: value})
}}/>

6.需要更新用户信息时,需要把用户原来的信息一起传回给后台,这个时候推荐下面的写法,首先

this.userState.user这个是数据源,然后...values 这个是表单中变化的值,通过新的值来覆盖旧的值,达到更新数据的操作。
if (tag === 'user') {
this.userState.updataUser({...this.userState.user,...values});//更新用户信息
}

5.业务场景:当用户添加一条数据,比如添加一个用户,但是用户名已经存在了,这个时候需要提示给用户并停留在当前的编辑界面,当操作成功后,跳转到table列表界面上

主要说明两点,其一异步操作,其二,state里面注意需要添加 return date的操作:

//state
//修改一条数据
async updataRoom(value) {
const {data} = await request(
{
method: 'put',
url: '/api/room',
data: value
},
{message: '保存成功'},
{message: '保存失败'},
);
this.setRoom(data);
return data;//重要
} //异步调用 handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
if (this.isAdd()) { //新增
this.roomState.createRoom(values).then((response)=> {
              //取到后台的返回 response 根据其中一个不为空的值做判断条件
const {status} = response;
debugger;
console.log('response',status);
if(status){
Utils.goBack();
}
}) } else {
values.roomId = this.props.match.params.id;
console.log('values of form: ', values);
this.roomState.updataRoom(values).then((response)=> {
console.log('response',response);
const {status} = response;
if(status){
Utils.goBack();
}
}); //修改
} }
});
};

4.执行删除操作后,界面重新查询一次,就是把删除后的效果呈现出来的操作模式

  /*
* 根据主机Id删除主机
* /api/host/{hostId}
* */
async deleteRemoveHost(hostId,cabinetId) {
const {data} = await request(
{method: 'DELETE', url: `/api/host/${hostId}/remove`},
{message: '移除成功'},
{message: '移除失败'},
);
this.getCabinetHost(cabinetId);
} }

3.当state状态发生改变会引发界面的重绘,即使数据改变会实现界面的重新渲染。这个思想比较重要,做说明一下(

this.setState({host: host});

)

constructor(props) {
super(props);
this.state ={
cabinetId: '',
hostId: '',
host:{},
};
this.cabinetState = this.props.store.cabinetState;
} /*
* 用户点击主机后,取出主机的详细信息展示在右侧
* */
handleClick = (host) =>{
console.log('主机参数',host);
this.state.host = host;
this.setState({host: host});
this.state.hostId = host.hostId;
console.log('当前主机',this.state.host.hostId);
//this.renderParameter(host)
}; renderParameter =()=>{
const hostObj = this.state.host;
if(hostObj === null || hostObj ===undefined || !hostObj.hostId){
return <div><h3 className="fgw-margin-bottom-20">请点击右侧主机,可查看主机的详情信息</h3></div>
}
console.log('主机参数',hostObj);
console.log('服务参数', eval('('+hostObj.info+')'));
//console.log('服务参数', JSON.stringify(hostObj.infos));
//console.log('服务参数', JSON.parse(hostObj.infos)); const info = eval('('+hostObj.info+')');
return(
<div>
<Col span={12}>
<label className="hl-font-20">设备名称:</label>
<span>{hostObj.hostId}</span>
</Col>
</div> )
)
};
  1. react提交按钮响应回车事件(基于react+Mobx的实现模式)

    handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
    if (!err) {
    console.log('Received values of form: ', values);
    if (e.keyCode == 13)//回车键的键值为13
    {
    e.returnValue=false;
    e.cancel = true;
    this.store.getPolicyPage(values);//调用按钮的事件
    }
    this.store.getPolicyPage(values);
    }
    });
    };
  2. renderHotFont =()=>{
    const hotFont = this.store.wordList;
    console.log("wordList",this.store.wordList);
    console.log("systemList",this.store.systemList.value); /*const sysList = [];
    for (itme of this.store.systemList){ }*/ const sysList = this.store.systemList.map(function (item) {
    return (item.value);
    });
    let myList = [];
    if(sysList.length > 0){//做非空判断
    myList = sysList[0].split(',');//字符串转成数组
    } if (!this.store.systemList) {
    return <div style={{textAlign: 'center', paddingTop: '30px'}}>暂无关键词</div>
    }
    return myList.map(doc => {
    const keyWord = this.store.wordList.find(temp => temp.id === doc);//从所有的关键词中获取对应的名字
    debugger;
    return (<div className="fgw-word">{keyWord.name}</div>)
    }); /*if (!this.store.wordList) {
    return <div style={{textAlign: 'center', paddingTop: '30px'}}>暂无关键词</div>
    }
    return this.store.wordList.map(doc => {
    return (<div className="fgw-word">{doc.name}</div>)
    });*/
    };
  3. //日期比较大小(开始时间不能大于结束时间约束)
    
    let params = {};
    params.startTime = values.startTime.format('YYYY-MM-DD');
    params.endTime = values.endTime.format('YYYY-MM-DD');
    console.log('开始时间', params);
    if(Date.parse(params.endTime) - Date.parse(params.startTime) <= 0){
    Modal.error({//使用模态框的error模式,需要引入对应的组件
    title:'提示消息',
    content:'开始时间不能大于结束时间'
    });
    return;
    }

react 问题记录二(侧重于state或者说server层操作)的更多相关文章

  1. React学习记录二

    环境基本弄清楚了以后,开始总会写个hello world什么的,开发做了这么久了,就跳过这一步吧. 还是从打开vscode说起吧,这里文件菜单打开一个文件夹Demos,查看菜单打开集成终端,也可以使用 ...

  2. 前端常用功能记录(二)—datatables表格(转)

    前端常用功能记录(二)—datatables表格 并不是所有的后台开发都有美工和前端工程师来配合做页面,为了显示数据并有一定的美感,jQuery的DataTables插件对于像我这样的前端菜鸟来说真是 ...

  3. Material Calendar View 学习记录(二)

    Material Calendar View 学习记录(二) github link: material-calendarview; 在学习记录一中简单翻译了该开源项目的README.md文档.接下来 ...

  4. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  5. React文档(六)state和生命周期

    想一下之前的章节时钟的例子. 目前为止我们只学习了一直方式去更新UI. 我们调用ReactDOM.render()方法去改变渲染的输出: function tick() { const element ...

  6. react基础语法(四) state学习

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. JavaScript学习记录二

    title: JavaScript学习记录二 toc: true date: 2018-09-13 10:14:53 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  8. react基础用法二(组件渲染)

    react基础用法二(组件渲染) 如图所示组件可以是函数 格式:function 方法名(){ return <标签>内容</标签>} 渲染格式: <方法名 />  ...

  9. 2.VUE前端框架学习记录二

    VUE前端框架学习记录二:Vue核心基础2(完结)文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baid ...

随机推荐

  1. Spring Cloud 之 Consul 知识点:服务注册与发现(类似工具:Eureka、ZooKeeper、Etcd)

    资料 网址 springcloud(十三):注册中心 Consul 使用详解 http://ityouknow.com/springcloud/2018/07/20/spring-cloud-cons ...

  2. 用 gradle 运行 spring boot 项目

    用 gradle 运行 spring boot 项目(网页中的第6章:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plug ...

  3. tomcat学习之路

    一:介绍目录文件夹 bin文件夹:放置可执行文件 startup.bat:脚本文件,windows环境,起服务 shutdown.bat:脚本文件,windows环境,关闭 startup.sh:脚本 ...

  4. BZOJ 3048: [Usaco2013 Jan]Cow Lineup 双指针

    看到这道题的第一个想法是二分+主席树(好暴力啊) 实际上不用这么麻烦,用一个双指针+桶扫一遍就行了 ~ code: #include <bits/stdc++.h> #define N 1 ...

  5. Numpy | 07 从数值范围创建数组

    numpy.arange ***** 使用numpy 包中的 arange 函数,创建数值范围并返回 ndarray 对象,函数格式如下: numpy.arange(start, stop, step ...

  6. vuex实现登录状态的存储,未登录状态不允许浏览

    基础思路就是使用vuex状态管理来存储登录状态(其实就是存一个值,例如token),然后在路由跳转前进行登录状态的判断,可以使用vue-router的全局前置守卫beforeEach,也可以使用路由独 ...

  7. X-factor Chain(信息学奥赛一本通 1628)

    题目描述 输入正整数 x,求 x 的大于 1 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及满足最大长度的序列的个数. 输入 多组数据,每组数据一行,包含一个正整数 x. 对于全部数据 ...

  8. 在spring boot里使用undertow而非tomcat

    参考https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-o ...

  9. 《京东B2B业务架构演变》阅读笔记

    一.京东 B2B 业务的定位 让各类型的企业都可以在京东的 B 平台上进行采购.建立采购关系. 京东 B2B 的用户群体主要分为 2 类: 一类是大 B 用户.另一类是小 B 用户.京东 B 平台需要 ...

  10. 如何设置github的ssh key

    目录 Generate SSH key in putty add key to github git setting How to add SSH key to github Generate SSH ...