lundi 31 juillet 2017

Reset Component react-history pattern

I'm trying to use to the react-history and especially the <Push /> Component to push a new entry in the history when I click on one of my menu items.

But maybe I want to do it too much in the declarative react way. When I see this in react-history docs, I don't really get it:

render() {
   const { to, ...props } = this.props;

   // If the <Link> was clicked, update the URL!
   if (this.state.wasClicked)
     return <Push path={to} />

   return (
     <Item {...props} onClick={() => this.setState({ wasClicked: true })} />
   )
}

They say in their docs that's not a good way, I understand that. The Push component gets rendered and the previous menu item disappears.

This may be ridiculous, but I'm guessing what is the proper pattern to use a Component like this, dependent on a user action? Splitting the behaviour in another component?

Aucun commentaire:

Enregistrer un commentaire