Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to set up routing in Meteor using react-router package and have encountered the following TypeError:

Link to image: https://postimg.org/image/v0twphnc7/

The code in I use in 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:

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.

like image 884
volna Avatar asked Jul 20 '16 14:07

volna


People also ask

Why is props undefined In react?

The "Cannot read property 'props' of undefined" error occurs when a class method is called without having the correct context bound to the this keyword. To solve the error, define the class method as an arrow function or use the bind method in the classes' constructor method.

What is match in REACT router?

A match object contains information about how a <Route path> matched the URL. match objects contain the following properties: params - (object) Key/value pairs parsed from the URL corresponding to the dynamic segments of the path. isExact - (boolean) true if the entire URL was matched (no trailing characters)

How do you define a react component?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML. Components come in two types, Class components and Function components, in this tutorial we will concentrate on Function components.


1 Answers

Assuming you are importing Component as a React.Component correctly, try removing the parenthesis after Component.

Should be:

class Portfolio extends Component { 

instead of:

class Portfolio extends Component () { 

If not, replace Componentwith React.Component.

like image 124
kctang Avatar answered Nov 13 '22 05:11

kctang