4

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

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>
    )
  }
}
2

3 Answers 3

1

You can call a components method from the outside as long as you keep a reference to the component. 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 component, 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.net/69z2wepo/48169/

Sign up to request clarification or add additional context in comments.

Comments

0

You cant call it from another component, because its a method belong to RegistrationModal component, 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

1 Comment

Doing a console.log seems to work, but how would I access the registrationModal ref in the RegistrationModal class?
0

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

A really great example and explanation can be found here: ReactJS Two components communicating

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.