简体   繁体   English

反应如何在按钮单击事件时进行呼叫模式

[英]react how to make call modal when button click event

I want to make when i click button, show modal in different class.我想当我点击按钮时,在不同的类中显示模态。 how to make it this?如何做到这一点?

import React, { Component } from 'react';
import addmodal from './addmodal';

class page extends Component {
  ...//skip
  handleAdd= () =>{
    //...how?
  }
  render(){
    return (
      <button onClick={this.handleAdd} > Add </button>
      )
   }
}


import React, { Component } from 'react';

class addmodal extends Component {
   // ... skip
  render(){
    return(
      <modal inOpen={this.state.isopen} >
        ...//skip
      </modal>
    )
  }
}

export default addmodal;

I can't call this addmodal... how to call modal?我不能称之为 addmodal ... 如何调用 modal?

First, your variable naming is terrible.首先,你的变量命名很糟糕。 I fixed it for you here.我在这里为你修好了。 The state that dictates if modal is open must be on the parent component since you have your trigger there.指示模态是否打开的状态必须在父组件上,因为您在那里有触发器。 Then, pass it as props to the modal component.然后,将其作为道具传递给模态组件。 Here is the fixed code for you:这是您的固定代码:

  import React, { Component } from 'react';
  import AddModal from './addmodal';

  class Page extends Component {
    constructor(){
      super();
      this.state = { isModalOpen: false };
    }
    ...//skip
    handleAdd= () =>{
      this.setState({ isModalOpen: true });
    }
    render(){
      return (
        <div>
          <button onClick={this.handleAdd} > Add </button>
          <AddModal isOpen={this.state.isModalOpen} />
        </div>
        )
    }
  }


  import React, { Component } from 'react';

  class AddModal extends Component {
    // ... skip
    render(){
      return(
        <modal inOpen={this.props.isOpen} >
          ...//skip
        </modal>
      )
    }
  }

  export default AddModal;

what about using trigger.使用触发器怎么样。 Its very usefull.它非常有用。

<Modal trigger={ <button onClick={() => onCLickPay()} className={someting}>  When clicked here modal with show up</button>  }`enter code here` >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM