最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Uncaught TypeError: Cannot read property 'func' of undefined - Stack Overflow

programmeradmin2浏览0评论

I'm receiving the error:

Uncaught TypeError: Cannot read property 'func' of undefined

Yet I have no idea why, I've Googled the error and gone to everyone post with the same error yet no luck. Can anyone help me out?

I'm using [email protected]

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import { Helmet } from 'react-helmet';

import Routes from './config/routes';

ReactDOM.render(
    <div>
        <Helmet>
            <meta charSet='utf-8'/>
            <title>Skelton</title>
            <link rel='icon' href='images/favicon.png'/>
            <link rel='stylesheet' href='style.css'/>
        </Helmet>
        <Router routes={Routes()} history={browserHistory}/>
    </div>
, document.getElementById('root'));

route.js

import React from 'react';
import { Route, IndexRoute } from 'react-router';

import Example1 from '../pages/Example1';

export function routes() {
    return (
        <Route>
            <Route path='/' component={Example1}/>
            <IndexRoute component={Example1}/>
        </Route>
    );
}

export default routes;

Example1.js

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Example1 extends Component {
    render() {
        return (
            <div>
                <h1>Hello World! This is Example 1.</h1>
            </div>
        );
    }
}

export default Example1;

Originally I didn't import PropTypes because I don't need it, yet.

I'm receiving the error:

Uncaught TypeError: Cannot read property 'func' of undefined

Yet I have no idea why, I've Googled the error and gone to everyone post with the same error yet no luck. Can anyone help me out?

I'm using [email protected]

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import { Helmet } from 'react-helmet';

import Routes from './config/routes';

ReactDOM.render(
    <div>
        <Helmet>
            <meta charSet='utf-8'/>
            <title>Skelton</title>
            <link rel='icon' href='images/favicon.png'/>
            <link rel='stylesheet' href='style.css'/>
        </Helmet>
        <Router routes={Routes()} history={browserHistory}/>
    </div>
, document.getElementById('root'));

route.js

import React from 'react';
import { Route, IndexRoute } from 'react-router';

import Example1 from '../pages/Example1';

export function routes() {
    return (
        <Route>
            <Route path='/' component={Example1}/>
            <IndexRoute component={Example1}/>
        </Route>
    );
}

export default routes;

Example1.js

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Example1 extends Component {
    render() {
        return (
            <div>
                <h1>Hello World! This is Example 1.</h1>
            </div>
        );
    }
}

export default Example1;

Originally I didn't import PropTypes because I don't need it, yet.

Share Improve this question asked Nov 8, 2017 at 15:57 user8826104user8826104 4
  • So, you aren't using prop -types, but you get that error, right? – Héctor Commented Nov 8, 2017 at 16:01
  • i dn think you are showing any code related to the error. can you find the location of the error from console and add the corresponding code snippets ? or the detailed error from console.. ? – Panther Commented Nov 8, 2017 at 16:01
  • @Panther the error is related to the code because originally I was doing <h1>Hello World!</h1> in index.jsx and it all worked perfectly fine. When I started to add routes that's when the problems occured. – user8826104 Commented Nov 8, 2017 at 16:16
  • @Héctor I get the error with and without PropTypes as an import. – user8826104 Commented Nov 8, 2017 at 16:16
Add a comment  | 

2 Answers 2

Reset to default 12

It looks like a react-router bug (related to prop types.) It's working on react-router 3.2.0

Check the issue here: https://github.com/ReactTraining/react-router/issues/5605

It's very easy to resolve.

This is happening because you are using react-router version 2.0.0

Just enter "npm install --save [email protected]"

It should just work fine now.

发布评论

评论列表(0)

  1. 暂无评论