Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

history missing in components [duplicate]

I just now observed that some of my component is having history and some do not have it. My component is comp1, comp2, comp3

comp1 is parent of comp2, comp2 is parent of comp3

comp1
|____comp2
     |_____comp3

Given all are stateful components. And access to comp1 is through routing.

My observations says that those component who are connected through routers directly have only the this.props.history available.

Q. Can any one throw some light here on this fact?

Q. Can we access history on remaining components?

like image 358
Ladoo Avatar asked Apr 28 '26 13:04

Ladoo


1 Answers

The components given to the component prop of a Route component will be given the route props. If you want a component deeper down in your app to have access to them as well, you can use the withRouter HOC.

Example

class Component3 extends React.Component {
  render () {
    console.log(this.props.history);

    return <h3> Test </h3>;
  }
}

export default withRouter(Component3);
like image 103
Tholle Avatar answered Apr 30 '26 01:04

Tholle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!