javascript - How can I call a component's method from another component in React? - Stack Overflow

I have a modal ponent with two methods that showhide the modal. How can I call those methods from anot

I have a modal ponent with two methods that show/hide the modal. How can I call those methods from another ponent?

This is the code for the Modal:

// Dependencies
//==============================================================================
import React from 'react'
import Modal from 'boron/DropModal'

// Class definition
//==============================================================================
export default class RegistrationModal extends React.Component {
  showRegistrationModal() {
    this.refs.registrationModal.show()
  }

  hideRegistrationModal() {
    this.refs.registrationModal.hide()
  }

  render() {
    return (
      <Modal ref="registrationModal" className="modal">
        <h2>Meld je aan</h2>
        <button onClick={this.hideRegistrationModal.bind(this)}>Close</button>
      </Modal>
    )
  }
}

I have a modal ponent with two methods that show/hide the modal. How can I call those methods from another ponent?

This is the code for the Modal:

// Dependencies
//==============================================================================
import React from 'react'
import Modal from 'boron/DropModal'

// Class definition
//==============================================================================
export default class RegistrationModal extends React.Component {
  showRegistrationModal() {
    this.refs.registrationModal.show()
  }

  hideRegistrationModal() {
    this.refs.registrationModal.hide()
  }

  render() {
    return (
      <Modal ref="registrationModal" className="modal">
        <h2>Meld je aan</h2>
        <button onClick={this.hideRegistrationModal.bind(this)}>Close</button>
      </Modal>
    )
  }
}
Share Improve this question asked Jul 7, 2016 at 11:14 noodles_ftwnoodles_ftw 1,3131 gold badge9 silver badges18 bronze badges 2
  • 1 Possible duplicate of ReactJS - Call One Component Method From Another Component – Damjan Pavlica Commented Nov 19, 2017 at 22:43
  • You can check it out github./burakozturk16/pigeon – Phd. Burak Öztürk Commented Nov 18, 2021 at 0:15
Add a ment  | 

3 Answers 3

Reset to default 1

You can call a ponents method from the outside as long as you keep a reference to the ponent. For example:

let myRegistrationModal = ReactDOM.render(<RegistrationModal />, approot );
    // now you can call the method:
    myRegistrationModal.showRegistrationModal() 

It's a bit cleaner if you pass a reference to the modal to another ponent, like a button:

let OpenModalButton = props => (
  <button onClick={ props.modal.showRegistrationModal }>{ props.children }</button>
);
let myRegistrationModal = ReactDOM.render(<RegistrationModal />, modalContainer );

ReactDOM.render(<OpenModalButton modal={ myRegistrationModal }>Click to open</OpenModalButton>, buttonContainer );

Working example: http://jsfiddle/69z2wepo/48169/

You cant call it from another ponent, because its a method belong to RegistrationModal ponent, but you can refactor your code so you can call it

export function hideRegistrationModal() {
  console.log("ok");
}

export default class RegistrationModal extends React.Component {
  render() {
    return (
      <Modal ref="registrationModal" className="modal">
        <h2>Meld je aan</h2>
        <button onClick={hideRegistrationModal}>Close</button>
      </Modal>
    )
  }
}

now you can call from anywhere but you need to import it first like this

import { RegistrationModal, hideRegistrationModal } from 'path to modal.js'
//       ^-- Component name  ^-- Method

What you want to do is create a parent ponent which will handle the munication between your modals.

A really great example and explanation can be found here: ReactJS Two ponents municating

This is a good approach because it keeps your ponents decoupled.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281560a4414582.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信