React点击事件,子组件只能首次获取数据,再次点击无法获取

Lampos 2018-09-19 07:18:58
问题具体描述如下:
如下图1点击管理然后弹出来一个模态框,模态框里面的基础信息图2是一个子组件(管理点击事件上带着数据id),子组件把传递过来的id拿到通过接口获取数据,
图1:

图2:


现在的问题先点ECU21测试-室外温度的管理可以正常获取数据,但是关闭了再点击电气柜的管理操作,所弹出来的数据没有改变。下面是相关代码和通过console.log打印的数据

代码-父组件:
	// 管理事件
onChange = (record,e) => {
//var devId = record.record.id;//当前设备id
e.preventDefault();

console.log('父组件');
console.log(record.record.id);
console.log('父组件');

this.setState({
currentDevId: record.record.id,
title:record.record.device_name,
visible:true
}, () => {
// this.setState({visible:true})
});
}


传递参数代码 - 通过devid传递:
			<Modal
title={"设备资料管理 - "+this.state.title}
width={850}
style={{ top: 20 }}
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
okText="保存"
cancelText="取消"
>
<Tabs>
<TabPane tab="基础信息" key="1" >
<BasicInfo devid={this.state.currentDevId} />
</TabPane>
<TabPane tab="运行参数" key="2"><RunParams devid={this.state.currentDevId} /></TabPane>
<TabPane tab="部件" key="3"><Part /></TabPane>
<TabPane tab="文档" key="4"><Document /></TabPane>
<TabPane tab="巡检" key="5"><Inspection /></TabPane>
<TabPane tab="下级设备" key="6"><DownEquip /></TabPane>
<TabPane tab="权限" key="7"><DeviceAuth /></TabPane>
</Tabs>
</Modal>


子组件:

// 获取设备信息
componentWillMount(){
console.log('子组件');
console.log(this.props);
console.log('子组件');

this.setState({
devId:this.props.devid,
userId:localStorage.userId,
organizationId:localStorage.organizationId
});

let formData = new FormData;

formData.append('devid',this.props.devid);

let opts = {method:'POST',body:formData};

fetch("http://api.test.net/?s=Device.getDeviceInfo",opts)
.then(response => response.json())
.then(json => {
console.log(json);
this.setState({datas:json.data});
})
}


下面是打印点击了三次打印的console.log数据


网上查询 了有说使用定时器或者 说setState第二个参数,这个是可以获取,但是没法执行相应操作,其它的也试了,但是还是不能获取到数据。希望各位大佬能为我解惑或者提供相关思路,不甚感激
...全文
812 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lampos 2020-05-22
  • 打赏
  • 举报
回复
引用 5 楼 imvirus 的回复:
[quote=引用 4 楼 Lampos 的回复:] [quote=引用 2 楼 qq_41114603 的回复:] https://blog.csdn.net/hesonggg/article/details/79373565 父组件调用子组件的代码,很简单
还是不行后来加了一个destroyOnClose={true}属性在modal上[/quote] destroyOnClose={true}可以,帮大忙了,感谢![/quote]
imvirus 2020-01-10
  • 打赏
  • 举报
回复
引用 4 楼 Lampos 的回复:
[quote=引用 2 楼 qq_41114603 的回复:] https://blog.csdn.net/hesonggg/article/details/79373565 父组件调用子组件的代码,很简单
还是不行后来加了一个destroyOnClose={true}属性在modal上[/quote] destroyOnClose={true}可以,帮大忙了,感谢!
Lampos 2018-09-20
  • 打赏
  • 举报
回复
这样是可以,但是如果把子组件包在tabs标签页里面,就获取不到子组件的事件里面,像是获取子组件快一步,那儿的东西没有准备好一样,因为打印是可以看到有,但是调用 就调用 不到
父组件调用子组件事件:
    onRef = (ref) => {
this.child = ref
}

Click = (e) => {
// this.child.myTest()
// this.setState({visible:true});
console.log(this.child);
console.log(this.child.myTest());
}


父组件页面view代码-这是1是放在外面,2是放里面,


子组件事件代码:
    componentDidMount(){
this.props.onRef(this)
}

myTest= () =>{
console.log('22222');
}



打印this.child和this.child.myTest的结果如下:

Lampos 2018-09-20
  • 打赏
  • 举报
回复
引用 2 楼 qq_41114603 的回复:
https://blog.csdn.net/hesonggg/article/details/79373565
父组件调用子组件的代码,很简单


还是不行后来加了一个destroyOnClose={true}属性在modal上
風灬雲 2018-09-19
  • 打赏
  • 举报
回复
https://blog.csdn.net/hesonggg/article/details/79373565
父组件调用子组件的代码,很简单
風灬雲 2018-09-19
  • 打赏
  • 举报
回复
子组件里面加一个事件,把componentWillMount()里面的数据处理逻辑放到事件里面,然后点击的时候通过父组件去触发子组件的这个方法,达到页面渲染的效果;componentWillMount()只会执行一次,你显示或者隐藏不会造成重绘

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧