How to hide a child component when changing React route

GluePear

I have a dropdown BasketContents component (as per this question), which fades in and out when a button is clicked. I also want it to close when a the route is changed by clicking a Link tag. I.e. if it's already open on the Checkout page, if I then click the Home page link I want it to automatically close without clicking the basket's close button.

My Routes.js is the root of the app:

render() {
    return (
      <Router history={hashHistory}>
        <Route path="/" component={App}>
          <IndexRoute component={Home} />
          <Route path="/home" component={Home} />
          <Route path="/checkout" component={Checkout} />
        </Route>
      </Router>
    );
}

App.js looks in part like this:

render() {
    const childrenWithProps = React.Children.map(
        this.props.children,
        child => {
            return (
                React.cloneElement(child, {
                    basket: this.state.basket
                    }
                )
            )
        }
    );
    return (
        <div className="main">
            <HeaderSection basket={this.state.basket} />
            <div className="content">
                {childrenWithProps}
            </div>
        </div>
    )
}

The Basket.js component contains the button:

constructor() {
    super();
    this.state = { open: false }
    this.handleDropDown = this.handleDropDown.bind(this);
}
handleDropDown() {
    this.setState({ open: !this.state.open })
}
render() {
    return(
        <div className="basket">
        <button className="basketBtn" onClick={this.handleDropDown}>Toggle</button>
        <BasketContents 
          contents={this.props.contents} 
          open={this.state.open} 
        />
        </div>
    )
}

I did try adding onEnter to my routes but understandably that doesn't do much since it doesn't have access to the Basket's open state. I would ideally like to set the open state of Basket.js to false when changing route (i.e. when clicking on a Link component). Is this possible?

GluePear

I found a solution that doesn't rely on context. The first thing I did, following the React docs, was to lift my basket state up from the Basket component to the App component. This made it easier to manipulate it from a central location.

Then, following a suggestion here, I listened for changes to the browserHistory in the componentWillMount hook in App:

componentWillMount() {
    browserHistory.listen( () =>  {
        this.setState({ basketOpen: false });
    });
}

The result is that when the page changes the basket closes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

React is updating both child component when data linked to one child is changing

From Dev

How to show/hide component in parent component via a button in a child component when using a map of buttons?

From Dev

How to show/hide component in parent component via a button in a child component when using a map of buttons?

From Dev

How to hide a parent component from a child component?

From Java

How to prevent parent component from reloading when changing a parameterised child component in Vue js

From Dev

React: How to hide a component by clicking on it?

From Dev

React state empty when changing route

From Dev

Angular Child Route not changing

From Dev

how to hide component is react and show another component?

From Dev

How do I hide a React component on the second click when the component is already open?

From Dev

Unrouted React component not updating when Route changes

From Dev

How to show and hide a Modal in child component from Parent component?

From Dev

How to show and hide a Modal in child component from Parent component?

From Dev

React: Calling two event handlers onClick and changing style of child component

From Dev

How to get useForm data in react component when input fields are in child component?

From Dev

React child component not updating when props change

From Dev

How should I go about changing the state of siblings when a child is clicked in React?

From Dev

How to access data in a child react component from a parent react component

From Dev

How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?

From Java

How to replace parent view with child route component in Vue

From Dev

Angular 2.0, how to detect route change on child component

From Dev

How to set a child component as a default route for a router-outlet?

From Dev

React: How to listen to child component events

From Dev

How to set the handler of a child component in React?

From Dev

How to change react component's child property

From Dev

React: how to pass a object inside a child component?

From Java

How to prevent child component from re-rendering when using React hooks and memo?

From Dev

React: How do I fix getting undefined when passing api data to child component using axios?

From Dev

Ember JS: How can a parent route/controller observe its child route/controller changing?

Related Related

  1. 1

    React is updating both child component when data linked to one child is changing

  2. 2

    How to show/hide component in parent component via a button in a child component when using a map of buttons?

  3. 3

    How to show/hide component in parent component via a button in a child component when using a map of buttons?

  4. 4

    How to hide a parent component from a child component?

  5. 5

    How to prevent parent component from reloading when changing a parameterised child component in Vue js

  6. 6

    React: How to hide a component by clicking on it?

  7. 7

    React state empty when changing route

  8. 8

    Angular Child Route not changing

  9. 9

    how to hide component is react and show another component?

  10. 10

    How do I hide a React component on the second click when the component is already open?

  11. 11

    Unrouted React component not updating when Route changes

  12. 12

    How to show and hide a Modal in child component from Parent component?

  13. 13

    How to show and hide a Modal in child component from Parent component?

  14. 14

    React: Calling two event handlers onClick and changing style of child component

  15. 15

    How to get useForm data in react component when input fields are in child component?

  16. 16

    React child component not updating when props change

  17. 17

    How should I go about changing the state of siblings when a child is clicked in React?

  18. 18

    How to access data in a child react component from a parent react component

  19. 19

    How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?

  20. 20

    How to replace parent view with child route component in Vue

  21. 21

    Angular 2.0, how to detect route change on child component

  22. 22

    How to set a child component as a default route for a router-outlet?

  23. 23

    React: How to listen to child component events

  24. 24

    How to set the handler of a child component in React?

  25. 25

    How to change react component's child property

  26. 26

    React: how to pass a object inside a child component?

  27. 27

    How to prevent child component from re-rendering when using React hooks and memo?

  28. 28

    React: How do I fix getting undefined when passing api data to child component using axios?

  29. 29

    Ember JS: How can a parent route/controller observe its child route/controller changing?

HotTag

Archive