最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Cannot read property 'props' of undefined - React Router - Stack Overflow

matteradmin5PV0评论

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>{'Wele to View1'}</h3>
    );
};


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

Any idea what might be wrong ?

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>{'Wele to View1'}</h3>
    );
};


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

Any idea what might be wrong ?

Share Improve this question asked Jan 7, 2017 at 14:26 iJadeiJade 23.9k58 gold badges161 silver badges250 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

arrow functions do not have lexical this. And functional ponents 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/ponents-and-props.html

Post a comment

comment list (0)

  1. No comments so far