Article From:https://segmentfault.com/q/1010000010040236
Question:

Parent component

class LocalizedModal extends React.Component {
  state = { visible: false }
  showModal = () => {
    this.setState({
      visible: true,
    });
  }
  hideModal = () => {
    this.setState({
      visible: false,
    });
  }
  render() {
    return (
      <div>
        <Button type="primary" onClick={this.showModal}>Modal</Button>
        <Modal
          title="Modal"
          visible={this.state.visible}
          onOk={this.hideModal}
          onCancel={this.hideModal}
          okText="Confirm "CancelText= "Cancel">< Child/>< /ModAl>< /div>);}}

ModalThe sub component Child in

class Child extends React.Component {
  componentDidMount() {
        console.log('componentDidMount')
        //do something
  }
  render() {
    return (
      <div>
        child
      </div>
    );
  }
}

The componentDidMount in the child component Child is only executed when the visible in the parent component becomes true. Is there any way to make the componentDidMount in the child component execute when the parent component is rendered, instead ofDo you have to wait for visible to change to true?

Answer 0:

this.state.visible && <Modal>…</Modal>

Answer 1:

Encountered the same problem, how did the landlord solve it?

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *