0

I'm writing a React app, namely a navbar. I have a logo and navItems there. Logo display and color of the navbar background are conditional. Added Aibnb guide to esLint, now my navbar code looks like this:

class Toolbar extends Component {
    handleClick = () => {
        this.props.history.push('/');
    }

    render() {
        const { color, showLogo } = this.props;
        return (
            <header
                style={{
                    backgroundColor: color,
                }}
                className={classes.Toolbar}
            >
                <Logo click={this.handleClick} show={showLogo} />
                <NavItems />
            </header>
        );
    }
}

Toolbar.propTypes = {
    color: PropTypes.string.isRequired,
    showLogo: PropTypes.string.isRequired,
};

export default withRouter(Toolbar);

and I get the following eslint errors:

eslint errors

(line 11) is

this.props.history.push('/');

How do I transform this method to make the code compile and work without errors?

(Tried to rewrite it in as a destructing props assignment, got an error like cannot get 'push' of undefined)

Dominik Domanski
  • 1,023
  • 2
  • 10
  • 18

2 Answers2

2

Setting the history PropType will help you get rid of the error:

Toolbar.propTypes = {
  color: PropTypes.string.isRequired,
  showLogo: PropTypes.string.isRequired,
  history: PropTypes.shape({
    push: PropTypes.func.isRequired,
  }).isRequired,
};
t3__rry
  • 2,817
  • 2
  • 23
  • 38
1

you should update

Toolbar.propTypes = {
color: PropTypes.string.isRequired,
showLogo: PropTypes.string.isRequired,
history:PropTypes.node
};

or

const {history} = this.props

Update

history:PropTypes.shape({push:PropTypes.func})

Yasin Tazeoglu
  • 884
  • 8
  • 14