0

I am using typescript and react router for navigation . And here is my start up file index.tsx

---- Other declarations 


if (module && module.hot) {
    module.hot.accept('./app.tsx', () => {
        ReactDOM.render(
            <AppContainer>
                <Router history={browserHistory}>
                  <Route path="/" component={App}>
                    <Route path="todo" components={{ main: AddTodo, sidebar: SidebarNav }}>
                      <Route path="add" component={AddTodo} />
                      <Route path="completed" component={CompletedTodos} />
                    </Route>
                  </Route>
                </Router>
            </AppContainer>,
            document.querySelector("#app")
        );
    });
}

In turn which results in rendering App Component ( App.tsx ) -

export class App extends React.Component < Children, any > {

    constructor(props: any) {
        super(props);
    }

    Click() {
        alert("This is awesome");
    }

    render(): JSX.Element {
        const { main, sidebar } = this.props;
        console.log(this.props);
        return (<div>
                <NavigationBar />
                <div className="row">
                    <div className="col s2 m2">
                        {sidebar || <SidebarNav/>}
                    </div>
                    <div className="col s10 m10">
                        {this.props.children}
                    </div>
                </div>
            </div>);
    }
}

And my sidebar component looks something like this sidebar.tsx -

export class SidebarNav extends React.Component < SidebarProps, any > {

    constructor(props: any) {
        super(props);
    }


    render() {
        return (
            <ul className="collapsible" data-collapsible="accordion">
                <li>
                    <div className="collapsible-header">
                        <i className="material-icons">filter_drama</i>Todo Actions
                    </div>
                    <div className="collapsible-body">
                        <ul className="collection">
                            <li className="collection-item">
                                <Link href='#' to={{ pathname: '/todo/add' }}>Add Todo</Link>
                            </li>
                            <li className="collection-item">
                                <Link href='#' to={{ pathname: '/todo/completed' }}>Completed Todo's</Link>
                            </li>                               
                        </ul>
                    </div>
                </li>                  
            </ul>

        );
    }
}

The issue is whenever I am trying to click the AddTodo link in the sidebar it shows me an error below . As far as I understand I am still under same router context, then why is it complaining about the links not being under same context . And what shall I do to get rid of this situation . Can anybody give me some solution / pointer to this issue ?

browser.js?9520:40 Uncaught Error: <Link>s rendered outside of a router context cannot navigate.
    at invariant (eval at <anonymous> (http://localhost:8888/main.bundle.js:2252:2), <anonymous>:40:15)
    at Object.handleClick (eval at <anonymous> (http://localhost:8888/main.bundle.js:2354:2), <anonymous>:103:79)
    at Object.ReactErrorUtils.invokeGuardedCallback (eval at <anonymous> (http://localhost:8888/main.bundle.js:1436:2), <anonymous>:70:16)
    at executeDispatch (eval at <anonymous> (http://localhost:8888/main.bundle.js:1430:2), <anonymous>:85:21)
    at Object.executeDispatchesInOrder (eval at <anonymous> (http://localhost:8888/main.bundle.js:1430:2), <anonymous>:108:5)
    at executeDispatchesAndRelease (eval at <anonymous> (http://localhost:8888/main.bundle.js:1424:2), <anonymous>:43:22)
    at executeDispatchesAndReleaseTopLevel (eval at <anonymous> (http://localhost:8888/main.bundle.js:1424:2), <anonymous>:54:10)
    at Array.forEach (native)
    at forEachAccumulated (eval at <anonymous> (http://localhost:8888/main.bundle.js:1448:2), <anonymous>:24:9)
    at Object.processEventQueue (eval at <anonymous> (http://localhost:8888/main.bundle.js:1424:2), <anonymous>:257:7)
Joy
  • 6,438
  • 8
  • 44
  • 75
  • Possible duplicate of [Programmatically navigate using react router](http://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router) – Andy Ray Jan 05 '17 at 17:50
  • @AndyRay No its not . Mine isn't programatically navigation . Its straight forward navigation through `` tag . But seems like the router is not getting to see the context properly ( may be I have made some mistake . I am new to react ! – Joy Jan 05 '17 at 17:52
  • Try removing `href="#"` on `` – Jyothi Babu Araja Jan 05 '17 at 17:54
  • @JyothiBabuAraja I tried now . Remains the same . – Joy Jan 05 '17 at 17:56
  • Once inspect the `Add Todo` link in your debugger and check it's `href` attribute – Jyothi Babu Araja Jan 05 '17 at 17:58
  • @JyothiBabuAraja I see . There are no href generated for that . the href attribute is missing now . – Joy Jan 05 '17 at 18:00
  • Nothing looks wrong to me. Have you verified that you can access the `context.router` within your `` component? (You'll need to add a `contextTypes` declaration to the component to do so). – Paul S Jan 05 '17 at 18:29
  • @PaulS `this.context` is coming as undefined. Not sure why . – Joy Jan 05 '17 at 18:39
  • You don't have any other `ReactDOM.render` calls in your code, correct? What you have said implies that the `` must be rendering outside of a router, but I don't see any evidence of that. Can you use the React Dev Tools extension to verify that your `` has a `` as a parent? (https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi) Right click and 'Inspect' the `Add Todo` link, go to the `React` tab in the Chrome Dev Tools, and check its component tree. – Paul S Jan 05 '17 at 18:55
  • @PaulS I am afraid you are right . I dont see the `` as parent for the `` component in react dev tools :( is that because I am passing it as a parameter from `` component ? – Joy Jan 05 '17 at 19:01
  • Do you see a `` at all in the component tree? – Paul S Jan 05 '17 at 19:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132431/discussion-between-joy-and-paul-s). – Joy Jan 05 '17 at 19:10

0 Answers0