iJade
iJade

Reputation: 23791

Cannot read property 'props' of undefined - React Router

I'm trying to do nested routing using react server. But I'm getting an error in the browser console stating

Uncaught TypeError: Cannot read property 'props' of undefined

Here is the react code

    const App = () => {
    return (
        <div>
            <h2>{'Nested Routing App'}</h2>
             <ul>
                <li>
                    <Link to="/view1">{'View1'}</Link>
                </li>
            </ul>
            {this.props.children}
        </div>
    );
};

const View1 = () => {
    return(
        <h3>{'Welcome to View1'}</h3>
    );
};


render(
    <Router>
        <Route component={App} path="/">
            <Route component={View1} path="/view1"></Route>
        </Route>
    </Router>,
document.getElementById('app'));

Any idea what might be wrong ?

Upvotes: 0

Views: 3774

Answers (1)

FlatLander
FlatLander

Reputation: 1757

arrow functions do not have lexical this. And functional components receive props as a function argument. So the right way to do this would be..

const App = (props) => {
    return (
        <div>
            <h2>{'Nested Routing App'}</h2>
             <ul>
                <li>
                    <Link to="/view1">{'View1'}</Link>
                </li>
            </ul>
            {props.children}
        </div>
    );
};

read more here https://facebook.github.io/react/docs/components-and-props.html

Upvotes: 6

Related Questions