I'm having difficulty utilizing an active state between two sibling components. I have NavComponent.jsx & HeaderComponent.jsx of which both render to different regions in the DOM.
I have a hamburger button that toggles the active state so it turns into an X while setting the menu state to active as well for the navigation. I was tasked with changing the interaction of the menu to push the content aside when the menu opens which meant I needed to break the header and navigation out into different components in the DOM. Now the active state works independently of each other when I'd like them to work together.
Someone told be about using a Redux Store but couldn't get that to work either.
Help would be very much appreciated.
NavComponent.jsx
import React from 'react';
const Navigation = (props) => (
<nav className={'navigation' + (props.active ? ' slide-in' : '')}>
<ul className="nav">
{
props.items.map(
(item, idx) => {
return (
<NavigationLink key={idx} href={item.href} text={item.text} clickHandler={props.clickHandler} />
);
}
)
}
</ul>
</nav>
);
export default class NavComponent extends React.Component {
constructor (props) {
super(props);
this.state = {
active: false
};
this.navItems = [
{
href: '/app/page1',
text: 'PAGE 1'
},
{
href: '/app/page2',
text: 'PAGE 2'
},
{
href: '/app/page3',
text: 'PAGE 3'
}
];
}
handleClick (e) {
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if (viewportWidth <= 767) {
this.setState({ active: !this.state.active });
}
}
render () {
return (
<Navigation active={this.state.active} items={this.navItems} clickHandler={this.handleClick.bind(this)} />
);
}
}
HeaderComponent.jsx
import React from 'react';
import Logo from '../img/logo.png';
import Logo2x from '../img/logo@2x.png';
import Logo3x from '../img/logo@3x.png';
const HamburgerToggle = (props) => (
<button className={'hamburger hamburger--squeeze' + (props.active ? ' is-active' : '')} onClick={props.clickHandler} type="button">
<span className="hamburger-box">
<span className="hamburger-inner"></span>
</span>
</button>
);
const BrandLogo = () => (
<a href="/app/page1" className="logo-link">
<img width="92" height="29" src={Logo} srcSet={Logo + ' 1x, ' + Logo2x + ' 2x, ' + Logo3x + '3x'} alt="Logo" className="logo" />
</a>
);
export default class HeaderComponent extends React.Component {
constructor (props) {
super(props);
this.state = {
active: false
};
}
handleClick (e) {
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if (viewportWidth <= 767) {
this.setState({ active: !this.state.active });
}
}
render () {
return (
<div>
<HamburgerToggle active={this.state.active} clickHandler={this.handleClick.bind(this)} />
<BrandLogo />
</div>
);
}
}