react中组件传值,执行关闭时修改父组件的数据,子组件不更新。

发布于 2022-09-13 00:36:41 字数 3084 浏览 11 评论 0

这个功能要怎么才能解决呀?
父组件:
image.png

点击这个数字执行render中的方法,将子组件中的弹窗visible值更改
image.png

它弹窗关闭看起来管用了,但是它会关闭这个弹窗之后再弹出来这个窗口,你再点击关闭,它才会彻底关上。
image.png

// state中的数据
meaStatColumn: [ // 采样点数列
      {
        title: '采样点数',
        dataIndex: 'meaNum',
        key: 'meaNum',
        render: (text, record) => {
          return (
            <a onClick={() => {
              this.handleCancel(record.colName, true)
            }}>{text}</a>
          )
        },
      },
    ]
    
// 更改弹窗状态的函数
  handleCancel = (colName, state) => {
    if (this.state.visible === false) {
      this.setState({
        visible: state,
      })
    }
    if (colName === '关闭') {
      this.setState({
        visible: state
      })
    }
    console.log('父组件:',this.state.visible,'子组件:',state); // 上面有图
  }
  
  // 子组件
   <MyTable
              rowClassName={(record, index) => {
                let className = 'light-row';
                if (index % 2 === 1) className = 'dark-row';
                return className;
              }}
              handleCancel={this.handleCancel}
              popupVisible={visible}
              column={devStatColumn}
              datas={devStat}
            ></MyTable>

子组件:

import React from 'react'
import { Table, Modal, Button, Space } from 'antd';
import ChildTable from '../../views/loadPage/ChildTable'
class MyTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: props.popupVisible,
      modalTitle: '详情',
    }
  }
  static getDerivedStateFromProps(props, state) {
    const visible = props.popupVisible || state.visible;
    if (visible !== state.visible) {
      return {
        visible,
      };
    }
    return visible;
  }
  handleBack = () => {
    const { handleCancel } = this.props;
    handleCancel();
  }

  Cancel = () => {
    this.props.handleBack('关闭', false)
    this.state.visible = false;
  };
  componentWillUnMount = () => {
    this.setState = (state, callback) => {
      return
    }
  }
  render() {
    const { visible, modalTitle, tableLoading } = this.state;
    const { column, datas } = this.props;
    return <>
      <Table
        className='my_table'
        bordered
        size={'small'}
        // scroll={{ y: 240 }}
        rowKey={record => record.colName + record.meaNum}
        pagination={false} // 分页器,设为 false 时不展示和进行分页
        columns={column}
        dataSource={datas}
        loading={tableLoading}
      />
      <Modal
        visible={visible}
        title={modalTitle}
        width={'80%'}
        onCancel={this.Cancel}
        footer={null}
      >
        <Button type='primary' style={{ marginBottom: '20px' }}>导出报表</Button>
        <ChildTable></ChildTable>
      </Modal>
    </>
  }
}
export default MyTable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2022-09-20 00:36:41

瞅的我强迫症都要犯了...所以为什么里面外面各要有个visible状态呢?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文