Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawer in React.js

Tags:

reactjs

I have a drawer component with a button that opens this drawer. I want to close the drawer by clicking everywhere on the page except drawer area. I have tried this code. It works for opening but it isn't working for closing.

var Child = React.createClass({
  render: function() {
    return (
        <div className="chatBar">
            <div onClick={this.onClick} className="closeBTN">
                <img src="../src/contents/images/svg/close.svg"/>
            </div>

            <Tab />
        </div>
        );
  }
});

var ChatBar = React.createClass({
  getInitialState: function () {
    return { childVisible: false ,childInVisible: true ,};

  },

  render: function() {
    return(
      <div>
        <div onClick={this.onClick} className="chatBTN">
         <img src="../src/contents/images/svg/chat.svg"/>
        </div>
        {
          this.state.childVisible
            ? <Child />
            : null
        }
      </div>
    )
  },

  onClick: function() {
    this.setState({childVisible: !this.state.childVisible});
  },

  onClickClose: function(){
    this.setState({childInVisible: !this.state.childInVisible});
  },
});

export default ChatBar;
like image 268
Yeganeh Salami Avatar asked Oct 27 '16 06:10

Yeganeh Salami


People also ask

What is drawer in react JS?

A panel that slides out from the edge of the page can replace Modal to present more content.

What is a drawer component?

A Drawer is a panel that is typically overlaid on top of a page and slides in from the side. It contains a set of information or actions.

What is drawer in MUI?

Material UI Drawer is the most widely used component of Material UI. Material UI Drawer is used as a navigation bar of the website which displays a list of items and then clicking on the item the user will be redirected to the specific part of the web page/website.


1 Answers

Please check the working demo JSFiddle.

var Child = React.createClass({
  render: function() {
    return (
        <div className="chatBar">
            <div onClick={this.props.onClick} className="closeBTN">
                <img src="http://www.freeiconspng.com/uploads/silver-close-button-png-15.png"/>
            </div>
        </div>
        );
  }
});

var ChatBar = React.createClass({
  getInitialState: function () {
    return { childVisible: false };

  },

  render: function() {
    return(
      <div>
        <div onClick={this.onToggle} className="chatBTN">
         <img src="http://www.omeglechat.eu/wp-content/uploads/2016/10/omegle-mnogochat.png"/>
        </div>
        {
          this.state.childVisible
            ? <Child onClick={this.onToggle.bind(this)} />
            : null
        }
      </div>
    )
  },

  onToggle: function() {
    this.setState({childVisible: !this.state.childVisible});
  }
});


React.render(<ChatBar />, document.body);

First, use one flag in the state:

  onToggle: function() {
    this.setState({childVisible: !this.state.childVisible});
  }

Secondly, in order to call a function (onClick) in the child component, you need to pass in the handler through <Child onClick={this.onToggle.bind(this)} />, and call it in the child component through onClick={this.props.onClick}

like image 151
Joy Avatar answered Nov 14 '22 10:11

Joy