Cannot read property of object

Uj Corb

I have an Article component and a state with all my articles. I map state to props with the right article I want to display.

console.log(props.article) works but console.log(props.article.title) or any other attribute does not (I get Cannot read property 'id' of undefined). why ?

My Article.js component:

import React from 'react';

import { connect } from 'react-redux';


const Article = props => (
<div className="aboutpage-wrapper">
  <div className="container">
{  console.log(props.article)}
{/*    <h2>{props.article.title}</h2>
    <h3>{props.articlesubtitle}</h3>
    <p>{props.article.body}</p>*/}
  </div>
</div>
);

const mapStateToProps = (state,props) => ({ article: state.articles.find(function (article) { return article.id === props.match.params.id; })});

export default connect(mapStateToProps)(Article);
Vikas Yadav

The answer drills down the unavailability of props and hence the respective collection inside them.

You should first check for the availability of your prop. You can use the ES6 destructuring operator as:

const { article } = this.props;

which is same as the

const article = this.props.article ? this.props.article : '';

Additionally, while rendering out the content inside you can use the same logic for the availability of content inside the article as:

<h2>{props.article.title ? props.article.title : '' } </h2>

Alternate Short-circuit logic:

While rendering the content, you can also write like this:

<h2>{(props.article || {}).title || ''} </h2>

I hope this will help.

Cheers!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot read property object reactJS

Ngrx : Cannot assign to read only property 'Property' of object '[Object]'

Cannot assign to read only property 'stopImmediatePropagation' of object

Console Log behaviour - Cannot read Object property

Cannot read property of an undefined object in a IF statement in JEST

Cannot assign to read only property 'detachedCallback' of object '#'

Cannot read property on Object (undefined?) inside instance

TypeError: Cannot read property 'object' of undefined in Yup

Angular can access to object but cannot read property

Cannot assign to read only property 'name' of object '[object Object]'

Cannot assign to read only property 'closed' of object '[object Object]'

Cannot assign to read only property 'x' of object '[object Object]'

Cannot read property 'push' of undefined creating an array property of an object (Javascript)

Cannot check for null if the object is null ("cannot read the property" error)

TypeError: Cannot assign to read only property 'statusKnown' of object '#<Object>'

TypeError: Cannot assign to read only property 'exports' of object '#<Object>' in ReactJS

Uncaught TypeError: Cannot assign to read only property 'whatever' of object '#<Object>'

Cannot assign to read only property 'whatever' of object #<Object>?

ReactJs - TypeError: Cannot assign to read only property 'transform' of object '#<Object>'

Javascript - TypeError: Cannot assign to read only property 'id' of object '#<Object>'

Uncaught TypeError: Cannot assign to read only property '' of object '#<Object>'

Cannot assign to read only property 'url' of object '#<Object>'

TypeError: Cannot assign to read only property 'quantity' of object '#<Object>'

Calling object method within the object—error: cannot read property of undefined

Uncaught TypeError: Cannot assign to read only property 'amount' of object '#<Object>'

js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball

Cannot read property on of undefined. Electron BrowserWindow object

Formik and Yup : TypeError: Cannot read property 'object' of undefined

Cannot read property ... of undefine , at Object.callbackFromNative In angular 2