搜尋

首頁  >  問答  >  主體

javascript - react 裡用antd model 怎麼控制model 開關

react 裡使用antd model 怎麼控制model 開關

父主鍵元件 傳入屬性

<AdvSimpleInfo visible={this.state.advSimpleInfoModel.visible}/>

advSimpleInfoModel 父元件state物件

#
advSimpleInfoModel: {
   visible: false
 }

子主鍵

#
import React from 'react';
import { Modal} from 'antd';

import Common from 'pricomp/Common';

import './advSimpleInfo.less'

export default  class AdvSimpleInfo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      confirmLoading: false
    };
  }
  showModal() {
    this.setState({
      visible: true
    });
  }
  handleOk() {
    let _this = this;
    this.setState({
      Loading: true
    });
    let params = {
      advertId: this.props.id
    };
    const promise = Common.ajax('pageQueryPrivilege', params);
    promise.then(function(res) {
      console.log(res);
    })
  }
  render() {
    return (
      <p>
        <Modal title="广告简单信息"
          visible={this.props.visible}
          onOk={this.handleOk}
          confirmLoading={this.state.confirmLoading}
          onCancel={this.handleCancel}>
          <p>{this.state.ModalText}</p>
          <p className="adv-simple-info-warp">
            <p><span>ID:</span>88888</p>
            <p><span>名称:</span>一个200红包</p>
            <p><span>状态:</span>有效</p>
            <p><span>库存:</span>剩余/总数</p>
            <p><span>广告主:</span>剩余/总数</p>
            <p><span>代理商:</span>剩余/总数</p>
          </p>
        </Modal>
      </p>
    );
  }
}

子元件屬性 改變了傳入的值 可是彈窗不能出現

visible={this.props.visible}
给我你的怀抱给我你的怀抱2736 天前990

全部回覆(2)我來回復

  • 我想大声告诉你

    我想大声告诉你2017-06-05 11:13:44

    看不到改變state裡面的visible的方法呀。不夠詳細唉。

    回覆
    0
  • 某草草

    某草草2017-06-05 11:13:44

    state 和 props 傻傻分不清,你的showModal這個方法也沒有綁定this,好好看看官網的例子:

    import { Modal, Button } from 'antd';
    
    class App extends React.Component {
      state = { visible: false }
      showModal = () => {
        this.setState({
          visible: true,
        });
      }
      handleOk = (e) => {
        console.log(e);
        this.setState({
          visible: false,
        });
      }
      handleCancel = (e) => {
        console.log(e);
        this.setState({
          visible: false,
        });
      }
      render() {
        return (
          <p>
            <Button type="primary" onClick={this.showModal}>Open</Button>
            <Modal
              title="Basic Modal"
              visible={this.state.visible}
              onOk={this.handleOk}
              onCancel={this.handleCancel}
            >
              <p>Some contents...</p>
              <p>Some contents...</p>
              <p>Some contents...</p>
            </Modal>
          </p>
        );
      }
    }
    
    ReactDOM.render(<App />, mountNode);

    回覆
    0
  • 取消回覆