I want to have active NavLink "submenu key" in React state to tell NavLink's onClick() method to check if any of NavLinks in Collapse is active and if so, don't toggle isOpen for Collapse.
1. I want to stop collapsing current Collapse if one of NavLink inside Collapse is active
2. Would be nice to have opened Collapse after refreshing site (of course if some NavLink inside is Active)
This code gives me Cannot update during an existing state transition (such as within 'render'). Render methods should be a pure function of props and state
error in console.
import React from 'react';
import { connect } from 'react-redux';
import { NavLink as Link } from 'react-router-dom';
import { Nav, NavItem, NavLink, Collapse } from 'reactstrap';
import { bindActionCreators } from 'redux';
import { actions } from './../../containers/Account/store';
import classnames from 'classnames';
class NavMenuAside extends React.Component {
constructor(props) {
super(props);
this.state = { collapsed: {} };
}
toggle(itemIndex) {
const { collapsed, active } = this.state
// Don't collapse if some NavLinks inside is active
// but allow to toggle() if is collapsed (if we refresh site)
if (active === itemIndex && collapsed[itemIndex]) {
return
}
this.setState({
collapsed: {
...collapsed,
[itemIndex]: !collapsed[itemIndex]
}
});
}
isActive = (itemIndex) => (match) => {
const { active } = this.state
if (match) {
if (active !== itemIndex) {
this.setState({
active: itemIndex
})
}
}
return !!match;
}
render() {
const { t } = this.props;
return (
<Nav className="nav--aside">
<NavItem>
<NavLink tag={Link} to="/admin/qqqq/ffff">{icon('envelope-colored', 'aside-svg')} {t('navMenu.alerts')}</NavLink>
</NavItem>
<NavItem>
<NavLink onClick={() => this.toggle(1)} className={classnames({ 'open': this.state.collapsed[1] })}>{icon('analytics', 'aside-svg')} {t('navMenu.terefere')}</NavLink>
<Collapse isOpen={this.state.collapsed[1]}>
<NavLink tag={Link} isActive={this.isActive(1)} to="/admin/wfwfwfwfwwf">{t('navMenu.terefere2')}</NavLink>
<NavLink tag={Link} isActive={this.isActive(1)} to="/admin/qdqdqd">{t('navMenu.terefere2')}</NavLink>
<NavLink tag={Link} isActive={this.isActive(1)} to="/admin/qqqqq">{t('navMenu.terefere2')}</NavLink>
<NavLink tag={Link} isActive={this.isActive(1)} to="/admin/wfwwfwf">{t('navMenu.terefere2')}</NavLink>
</Collapse>
</NavItem>
</Nav>
);
}
}