简体   繁体   English

React-router:TypeError:无法设置未定义的属性“props”

[英]React-router: TypeError: Cannot set property 'props' of undefined

I am trying to set up routing in Meteor using react-router package and have encountered the following TypeError :我正在尝试使用react-router包在Meteor中设置路由,但遇到了以下TypeError

Link to image: https://postimg.org/image/v0twphnc7/图片链接: https ://postimg.org/image/v0twphnc7/

The code in I use in main.js我在main.js中使用的代码

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';

// Importing components
import App from './components/app';
import Portfolio from './components/portfolio/portfolio';


//Creating a route
const routes = (
  <Router history={browserHistory}>
    <Route path='/' component={App}>
      <Router path='portfolio' component={Portfolio} />
    </Route>
  </Router>
);


// Loading routes
Meteor.startup(() => {
  ReactDOM.render(routes, document.querySelector('.universe'));
});

The problem that I've managed to identify is that when I define portfolio as a Simple Component it works.我设法确定的问题是,当我将投资组合定义为简单组件时,它会起作用。

const Portfolio = () => {
    return (
        <div className='red'>Portfolio page</div>
    );
}

But when I extend it from the Component is where the error comes in:但是当我从 Component 扩展它时,错误就出现了:

class Portfolio extends Component () {
  render() {
    return (
        <div>Portfolio page</div>
    );
  }
}


Can you please explain the possible difference between "normal" and class component and why the following error appears.您能否解释一下“普通”组件和类组件之间可能存在的差异以及出现以下错误的原因。

Assuming you are importing Component as a React.Component correctly, try removing the parenthesis after Component.假设您正确地将Component作为React.Component导入,请尝试删除 Component 后的括号。

Should be:应该:

class Portfolio extends Component {

instead of:代替:

class Portfolio extends Component () {

If not, replace Component with React.Component .如果不是,请将Component替换为React.Component

I am trying to set up routing in Meteor using react-router package and have encountered the following TypeError :我正在尝试使用react-router包在Meteor设置路由并遇到以下TypeError

Link to image: https://postimg.org/image/v0twphnc7/图片链接: https : //postimg.org/image/v0twphnc7/

The code in I use in main.js我在main.js中使用的代码

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';

// Importing components
import App from './components/app';
import Portfolio from './components/portfolio/portfolio';


//Creating a route
const routes = (
  <Router history={browserHistory}>
    <Route path='/' component={App}>
      <Router path='portfolio' component={Portfolio} />
    </Route>
  </Router>
);


// Loading routes
Meteor.startup(() => {
  ReactDOM.render(routes, document.querySelector('.universe'));
});

The problem that I've managed to identify is that when I define portfolio as a Simple Component it works.我设法确定的问题是,当我将投资组合定义为简单组件时,它会起作用。

const Portfolio = () => {
    return (
        <div className='red'>Portfolio page</div>
    );
}

But when I extend it from the Component is where the error comes in:但是当我从 Component 扩展它时,错误就出现了:

class Portfolio extends Component () {
  render() {
    return (
        <div>Portfolio page</div>
    );
  }
}


Can you please explain the possible difference between "normal" and class component and why the following error appears.你能解释一下“正常”和类组件之间可能的区别,以及为什么会出现以下错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现“ TypeError:无法读取未定义的属性” props”(反应路由器,React-Redux)? - Why am I getting 'TypeError: Cannot read property 'props' of undefined' (react-router, React-Redux)? 反应路由器:未捕获的TypeError:无法读取未定义的属性&#39;func&#39; - React-router: Uncaught TypeError: Cannot read property 'func' of undefined react-router Uncaught TypeError:无法读取未定义的属性'toUpperCase' - react-router Uncaught TypeError: Cannot read property 'toUpperCase' of undefined React-Router-未捕获的TypeError:无法读取未定义的属性“ route” - React-Router - Uncaught TypeError: Cannot read property 'route' of undefined React - TypeError:无法设置未定义的属性'props' - React - TypeError: Cannot set property 'props' of undefined 未捕获的TypeError:无法在javascript / React-router / React中读取未定义的属性&#39;forEach&#39; - Uncaught TypeError: Cannot read property 'forEach' of undefined in javascript / React-router / React 类型错误:从 react-router 使用 useParams 时,无法读取未定义的属性“匹配” - TypeError: Cannot read property 'match' of undefined when using useParams from react-router 如何解决react-router中的“ TypeError:无法读取未定义的属性&#39;push&#39;”? - How to solve “TypeError: Cannot read property 'push' of undefined” in react-router? 无法读取未定义的属性“ props”-React Router - Cannot read property 'props' of undefined - React Router React - TypeError:无法读取未定义的属性“道具” - React - TypeError: Cannot read property 'props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM