reactjs - Target element from another component onClick - React JS -
i have toggle icon in header.js component on click want change class of element in sidebar component in order change width , minimize half width. best way can react? have togglesidebar function on header component checks state , changes onclick, im not sure go here?
header.js :
import react "react"; export class header extends react.component { constructor(props) { super(props); this.togglesidebar = this.togglesidebar.bind(this); this.state = { istoggled: false } } togglesidebar() { if(this.state.istoggled) { this.setstate({ istoggled: false }) } else { this.setstate({ istoggled: true }) } } render() { return ( <nav classname="navbar navbar-toggleable-md navbar-light bg-faded"> <button classname="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsupportedcontent" aria-controls="navbarsupportedcontent" aria-expanded="false" aria-label="toggle navigation"> <span classname="navbar-toggler-icon"></span> </button> <a classname="navbar-brand" href="#" onclick={this.togglesidebar}><span classname="fa fa-bars" aria-hidden="true"></span></a> <div classname="collapse navbar-collapse" id="navbarsupportedcontent"> <ul classname="navbar-nav ml-auto"> <li classname="nav-item active"> <a classname="nav-link" href="#"><img alt="user dp" classname="header__user__img img-fluid rounded-circle" src="../resources/img/user.jpg" /> mark hughes</a> </li> </ul> </div> </nav> ); } }
sidebar.js :
import react "react"; import sidebarcategories "./sidebarcategories"; export class sidebar extends react.component { render() { return ( <div classname="sidebar col-sm-2"> <div classname="row"> <div classname="sidebar__header col"> <img alt="logo" classname="img-fluid sidebar__header__logo" src="../resources/img/logo-white.png" /> {'\u00a0'} <h4 classname="sidebar__header__title i-block">title</h4> </div> </div> <div classname="row"> <div classname="sidebar__user container-fluid"> <div classname="row"> <div classname="col-sm-4 text-center"> <img alt="user dp" classname="sidebar__user__img img-fluid rounded-circle" src="../resources/img/user.jpg" /> </div> <div classname="col-sm-8"> <p><strong>welcome</strong><br /> mark hughes</p> </div> </div> </div> </div> <div classname="row"> <sidebarcategories /> </div> </div> ); } }
Comments
Post a Comment