Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Component returning "TypeError: undefined has no properties"

I have a component right below which is later used inside another component in react.

Here is the code:

import React, { PropTypes } from 'react';

const MyComponent = () => (
  <div>
    <div className="row">
      <div className="col-md-12">
        <div className="media">
          <div className="media-left media-middle">
            <a href="#no"><img className="media-object" src={this.props.image} alt="" /></a>
          </div>
          <div className="media-body"><span className="pull-right">{this.props.name}</span>
            <h4 className="media-heading">{this.props.title}</h4>
            {this.props.desc}
          </div>
        </div>
      </div>
    </div>
  </div>
);

MyComponent.propTypes = {
  image: PropTypes.string,
  name: PropTypes.string,
  title: PropTypes.string,
  desc: PropTypes.string,
};

export default MyComponent;

//Then on other component

import React from 'react';
import MyComponent from './mycomponent.component';

<MyComponent
  title="Title Here"
  name="Some Name"
  desc="Description here"
  image="http://placehold.it/100x100"
/>

The problem is that it's not rendering and the developer console is returning the following error:

"TypeError: undefined has no properties"

What is the problem here and how can I fix this issue?


1 Answers

You have to pass in props to MyComponent like so:

const MyComponent = (props) => (
     console.log("example prop is", props.image);
);
like image 52
Frosty619 Avatar answered May 19 '26 08:05

Frosty619