react this.props.form异步执行问题
最近在做一个选择器联动时,碰到this.props.form的异步执行问题,导致选择器一直没有办法联动
如图,选择公司名称后,应该同步刷新门店选择默认值,
但同时又要清空门店选择的上一次记录
就用到了this.props.form中的setFieldsValue()方法来清空,但是this.props.form却是在最后才执行,导致选择的默认值 一直为空
上代码:
var paramsNew = {};
let order = React.createClass({
mixins: [LoadingMixin,NotificationMixin,RequestMixin],
getInitialState(){
return {
data: [],
param:{}, }
},
componentWillMount(){
this.companyList()
},
componentWillUnmount(){ }, companyList(){
this.get({ //公司列表
url:"Api/lists/*******bac",
param: {
},
noLoading: true
}).then(result=> {
this.setState({
tableCompanyName: result.result || [],
},this.shopsList)
});
}, shopsList(){
this.get({ //门店列表
url: "Api/lists/******af4bac",
param: {
companyid:this.state.param.companyid ? this.state.param.companyid :'',
},
noLoading: true
}).then(result=> { if(result.result == null){
paramsNew.shopid = '' }else {
paramsNew.shopid = result.result[0].id }
this.setState({shopsList: result.result || [],param:paramsNew}); });
}, companyChange(value){
console.log("cpanyChange-?*--",value);
this.props.form.setFieldsValue({
shopid:'' //此步一直异步执行,每次都是在最后才清空,导致请求的数据 的默认选择中值 一直为空
})
paramsNew.companyid = value;
paramsNew.shopid = ""
// console.log("paramsNew--------11-------",paramsNew);
this.setState({
param: paramsNew,
},this.shopsList)
}, render() {
const { getFieldDecorator } = this.props.form;
return (
<div className="order-main">
<div className="title">
<h2>订单管理</h2>
</div>
<div className="form-search">
<Form layout="inline" onSubmit={this.handleSubmit} autoComplete="off">
<FormItem>
{
getFieldDecorator('companyid',{
initialValue: this.state.param && this.state.param.companyid || '',
})(
<Select style={{ width: '120px' }}
onChange={this.companyChange}
disabled={this.state.tableCompanyName.length == 1 ? true: false}
>
<Option value=""> --公司名称-- </Option>
{
this.state.tableCompanyName && this.state.tableCompanyName.map((item,index) => {
return (
<Option key={item.id} value={item.id}> {item.title}</Option>
)
})
}
</Select>
)
}
</FormItem> <FormItem>
{
getFieldDecorator('shopid',{
initialValue: this.state.param && this.state.param.shopid || '', })(
<Select style={{ width: '120px' }} >
{
this.state.shopsList && this.state.shopsList.map((item,index) => {
return (
<Option key={item.id} value={item.id}> {item.title}</Option>
)
})
}
</Select>
)
}
</FormItem> <Button type="primary" htmlType="submit">查询</Button>
{/*<Button onClick={this.handleReset}>重置</Button>*/}
</Form>
</div>
<div> </div>
</div>
)
}
});
order = createForm()(order);
export default order;
想了下,将表单字段 清空方法放进数据 请求中清空,才解决这个问题
var paramsNew = {};
let order = React.createClass({
mixins: [LoadingMixin,NotificationMixin,RequestMixin],
getInitialState(){
return {
data: [],
param:{}, }
},
componentWillMount(){
this.companyList()
},
componentWillUnmount(){ }, companyList(){
this.get({ //公司列表
url:"Api/lists/*******bac",
param: {
},
noLoading: true
}).then(result=> {
this.setState({
tableCompanyName: result.result || [],
},this.shopsList)
});
}, shopsList(){
this.get({ //门店列表
url: "Api/lists/******af4bac",
param: {
companyid:this.state.param.companyid ? this.state.param.companyid :'',
},
noLoading: true
}).then(result=> { if(result.result == null){
paramsNew.shopid = ''
this.props.form.setFieldsValue({
shopid:'' //将清空方法放到此处清空,可以解决异步问题
})
}else {
paramsNew.shopid = result.result[0].id
this.props.form.setFieldsValue({
shopid:result.result[0].id //将清空方法放到此处直接赋值,可以解决异步问题
})
}
this.setState({shopsList: result.result || [],param:paramsNew}); });
}, companyChange(value){
console.log("cpanyChange-?*--",value);
// this.props.form.setFieldsValue({
// shopid:''
// })
paramsNew.companyid = value;
paramsNew.shopid = ""
// console.log("paramsNew--------11-------",paramsNew);
this.setState({
param: paramsNew,
},this.shopsList)
}, render() {
const { getFieldDecorator } = this.props.form;
return (
<div className="order-main">
<div className="title">
<h2>订单管理</h2>
</div>
<div className="form-search">
<Form layout="inline" onSubmit={this.handleSubmit} autoComplete="off">
<FormItem>
{
getFieldDecorator('companyid',{
initialValue: this.state.param && this.state.param.companyid || '',
})(
<Select style={{ width: '120px' }}
onChange={this.companyChange}
disabled={this.state.tableCompanyName.length == 1 ? true: false}
>
<Option value=""> --公司名称-- </Option>
{
this.state.tableCompanyName && this.state.tableCompanyName.map((item,index) => {
return (
<Option key={item.id} value={item.id}> {item.title}</Option>
)
})
}
</Select>
)
}
</FormItem> <FormItem>
{
getFieldDecorator('shopid',{
initialValue: this.state.param && this.state.param.shopid || '',
})(
<Select style={{ width: '120px' }} >
{
this.state.shopsList && this.state.shopsList.map((item,index) => {
return (
<Option key={item.id} value={item.id}> {item.title}</Option>
)
})
}
</Select>
)
}
</FormItem> <Button type="primary" htmlType="submit">查询</Button>
{/*<Button onClick={this.handleReset}>重置</Button>*/}
</Form>
</div>
<div> </div>
</div>
)
}
});
order = createForm()(order);
export default order;
react this.props.form异步执行问题的更多相关文章
- React的useEffect与useLayoutEffect执行机制剖析
引言 useEffect和useLayoutEffect是React官方推出的两个hooks,都是用来执行副作用的钩子函数,名字类似,功能相近,唯一不同的就是执行的时机有差异,今天这篇文章主要是从这两 ...
- Python开发程序:RPC异步执行命令(RabbitMQ双向通信)
RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...
- React(17)异步组件
26.异步组件当在React里使用异步组件时,核心知识是两个: webpack 如何异步加载其他模块:通过 require(['xxx'], function(module){})来实现:React ...
- 附实例!图解React的生命周期及执行顺序
本文由云+社区发表 作者:前端林子 1.七个可选的生命周期 可以结合下图来看: (1) componentWillMount() 仅在render()方法前被调用一次,如果在该方法中调用了setSta ...
- React中props
今天让我们开启新的篇章好吧,来搞一搞React,以下所有操作都是我个人的一些理解,如果有错吴还请指出,想要看更全的可以去React官网可能一下子好吧 昨天按摩没到位,导致今天身体不太行,撸码千万别苦了 ...
- React中Props 和 State用法
React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
- react生命周期获取异步数据
当react组件需要获取异步数据的时候,建议在ComponentDidMount周期里执行获取动作, 如果非异步数据,可以在ComponentWillMount获取 因为ComponentWillMo ...
- Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)
RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...
- sql异步执行
/// <summary> /// 按钮事件 异步执行 /// </summary> /// <param name="sender">< ...
随机推荐
- android中Activity中的WindowManager与Window
在做项目的过程中,需要实现Activity非全屏显示.窗口背景透明显示的效果. 在实现这些功能的过程中,涉及到Window与WindowManager两个类,经过查一些相关资料,了解二者之间的不同点如 ...
- iOS 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃
我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...
- Python 快速部署安装所需模块
需求 我们需要在拷给别人或者提交至服务器也用同样的模块,好保持和开发的一样,所以我们需要自己手动写配置模块信息. 方法 在根目录下创建一个 requirements.txt 文件 里面写 模块名== ...
- CSU-2110 Keeping Cool
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2110 题目 Description Kevin has just got ...
- Python 的音乐库
前言 其实处理这个用 Matlab 最方便,之前把 guitar-synthesizer 从 Matlab 移植到 Python,过程中更是体会到了这一点. 不过 Matlab 安装包又大,启动又慢, ...
- C# 命名管道
有些场合需要高效率,进行线程间通信,可以使用 C#命名管道.
- 后端model传入前端JSP页面中的值判断后再取值
所遇到的问题后端model传入前端JSP页面中的值通过foreach循环内要满足条件才能取值给Div中,我们知道jsp页面中可以直接用EL表达式取值,格式就是${"model中传来的数据&q ...
- nginx禁止访问目录中可执行文件
某些网站系统需要用户上传图片等文件到某些目录下,难免程序有些漏洞,导致用户上传了php.cgi等等可执行的文件,导致网站陷入非常为难的境地. 此时我们可以通过nginx来禁止用户访问这些目录下的可执行 ...
- JS计算器(自制)
<!doctype html><html><header><meta charset="utf-8"><script src= ...
- 前端构建工具gulpjs的使用介绍及技巧 (转)
gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常简单,学习起来很容易,而且gulpjs使用的是nodejs中stream来读取和操作数据,其速 ...