5

I'm trying to create a simple configuration dialog with close icon on top-right, but can't think of how to handle this in React. In other frameworks, I can simply use selector, and then use .showModal()/close() to open/close a dialog. However, I think we're not allowed, or not recommended to directly manipulate DOM in React, so I wonder what's the proper way in this case.

My project outline

App.js

class App extends Component {
  ...
  ...
  return(
    <div>
      <ConfigPage />
      <ConfigButton />
      <MainContents />
    </div>
  )
}

I want to open a dialog, which is <ConfigPage />, by pressing the <ConfigButton /> I set, and close it by pressing the icon on the dialog.

config-page.js

class ConfigPage extends Component {
  ...
  ...
  return(
    <dialog>
      <header>
        <div>
          <i onClick={someCallback}></i>               
        </div>
      </header>
      <section></section>
    </dialog>
  )
}

2 Answers 2

4

the HTML5 dialog also has an open attribute, correct? Instead of calling show/hide you could manipulate this attribute -

class ConfigPage extends Component {
  ...
  ...
  return(
    <dialog open={this.state.showDialog ? 'open' : false}>
      <header>
        <div>
          <i onClick={someCallback}></i>               
        </div>
      </header>
      <section></section>
    </dialog>
  )
}

And when you want to show/hide call this.setState({showDialog: true}) (or false)

Here's a js fiddle with a proof-of-concept: https://jsfiddle.net/n5u2wwjg/193969/

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

7 Comments

Hello, is there anything might cause open attribute can't be removed properly? I added a state showDialog, and did () => {this.setState({showDialog: false})} inside the <i onClick={...here...}>, but it didn't remove the open attribute. p.s. I opened the dialog by doing show call in console.
Can you post your render method?
If you're showing/hiding the dialog from the console as well it will throw off the react component because the state will no longer match the dialog's state.
open attribute won't trap focus, you shall use showModal
According to html spec documentation and MDN: "Opening dialogs via HTMLDialogElement.show() is preferred over the toggling of the boolean open attribute."
|
1

Welcome to SO. You can hide a react component by return null from the render function. You can define a flag in the state that determines weather or not your component is visible. Here is a simple example.

class Modal extends Component {

  constructor(props) {
    super(props);
    this.state = {
      isOpen: true;
    };
    this.onCloseClick = this.onCloseClick.bind(this);
  }


  onCloseClick(e) {
    e.preventDefault();
    this.setState({
      isOpen: false,
    });
  }


  render(){
    if (!this.state.isOpen) return null;
    return (
      <div>
        <button onClick={this.onCloseClick}>
          Close
        </button>
        <h1>What up, this a modal</h1>
      <div>
    );
  }

}

1 Comment

Thanks for your approach! It works fine, but the other answer is closer to my idea.

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.