ReactTraining / react-history

Manage session history with React
266 stars 23 forks source link

Usage of <Push />, <Pop />,... #21

Closed unkndono closed 7 years ago

unkndono commented 7 years ago

Hi @mjackson,

I'm a bit embarrassed to ask you that, maybe I'm taking the problem in the wrong way. I'm trying to use the <Push /> component after clicking on one of my menu items. When I see this in react-history docs, I don't really get that part:

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

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

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

The <Push /> component gets rendered and the previous menu span disappears.

This may be ridiculous, but I'm guessing on what is the proper pattern to use a component like this, which has a one-shot action (and should be destroyed ?)? Putting it in an another component, which I will destroy after the behaviour done?

Maybe it will help somebody else who is struggling like me.

mjackson commented 7 years ago

The idea here is that as soon as you render a <Push> the URL changes and the component that rendered the <Push> probably won't even render anymore. But it's a little questionable because even at the new URL that component may still render, in which case you'd probably get stuck in a loop.

Anyway, this library is highly experimental. If you're looking for a general purpose navigation library, I'd stick with the APIs we provide in react-router.