Skip to content Skip to sidebar Skip to footer

How To Pass Component Reference To Routes Component In React

In react i want to pass a reference of component to routes component . But when i route to path such as '/' it re render the passed component so starts with 0 again. Here is a Lin

Solution 1:

You can’t pass a component by “reference”. However, if you would like to have the same data between all the components you could initialize your state in the parent component (BasicExample) and pass it down or use a state container like Redux.

Solution 2:

You can the component down but the component will also go through a mounting process that will invalidate the state that you have stored in it. If you need to hold the state, it should be stored in the parent component.

constRouter = ReactRouterDOM.HashRouter;
constRoute = ReactRouterDOM.Route;
constLink = ReactRouterDOM.Link;

classDummyextendsReact.Component {
  constructor(props) {
    super(props);
    this.state = {
      c: props.c || 0
    };
  }
  componentWillReceiveProps(nextProps) {
    if (this.props.c !== nextProps.c) {
      this.setState({
        c: nextProps.c
      });
    }
  }
  render() {
    return<h1> Counter {this.state.c}</h1>;
  }
}

classBasicExampleextendsReact.Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 0
    };
    setInterval(() => {
      this.setState(prevState => {
        return {
          counter: prevState.counter + 1
        };
      });
    }, 1000);
  }
  render() {
    letCom = <Dummyc={this.state.counter} />;
    return (
      <div><h2>Main App</h2><Router><div>
            {Com}
            <ul><li><Linkto="/">Home</Link></li><li><Linkto="/about">About</Link></li></ul><hr /><Routeexactpath="/"
              // render={() =><Homecom={com} />} //this also not work
              render={props => (
                <Home {...props} c={this.state.counter}Com={Dummy} />
              )}
            />
            <Routepath="/about"render={props => (
                <About {...props} c={this.state.counter}Com={Dummy} />
              )}
            />
          </div></Router></div>
    );
  }
}

classHomeextendsReact.Component {
  render() {
    constCom = this.props.Com;
    return (
      <div><h2>Home </h2><Comc={this.props.c} /></div>
    );
  }
}

classAboutextendsReact.Component {
  constructor(props) {
    super(props);
  }
  render() {
    constCom = this.props.Com;
    return (
      <div><h2>About</h2><Comc={this.props.c} /></div>
    );
  }
}

ReactDOM.render(<BasicExample />, document.getElementById("root"));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react-router/4.2.0/react-router.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/4.2.2/react-router-dom.js"></script><divid="root"></div>

Few things to note here are:

  1. Use render property of Route to pass in any props

When you use component (instead of render or children, below) the router uses React.createElement to create a new React element from the given component. That means if you provide an inline function to the component prop, you would create a new component every render. This results in the existing component unmounting and the new component mounting instead of just updating the existing component. When using an inline function for inline rendering, use the render or the children prop

  1. Use function version of setState to refer to prevState

Post a Comment for "How To Pass Component Reference To Routes Component In React"