ISAAC
ISAAC

Reputation: 159

Passing Data Between React Classes doesn't work

I'm trying to pass data between classes and it's not working. The error says: "Cannot read property 'map' of undefined" (I need it to stay a class for a future state). I have tried to use it in a different component (not a Class) and it works.

This is my first class:

import React, { Component } from 'react';
import ArticlePage from './components/articlePage';
import './App.css';

class App extends Component {
    constructor(props) {
      super(props);
      this.state = {
        articles: '',
        newArticleList: []
      }
    }

componentDidMount(){
  fetch('https://example.com/wpjson/wp/v2/od_article_embed&page=1&per_page=10')
  .then(response => response.json())
  .then(myJson => this.setState({articles: myJson.map(articleName => articleName )}))
}

componentDidUpdate(prevProps, prevState) {
  if (this.state.articles !== prevState.articles) {
    this.setState({newArticleList: this.state.articles.map(article => {
      return (
          [{image: article._embedded['wp:featuredmedia']['0'].source_url}
      )
    })})
  }
}

render() {    
    return (
        <div className="App">
            <ArticlePage 
              allArticles={this.state.newArticleList}/>
        </div>
    );
  }
}

export default App;

This is the class I'm trying to forward it to:

import React, {Component} from 'react';

class ArticlePage extends Component {
    constructor(props) {
        super(props);
        this.state = {

        }
    }

render() {
    return (
        <div className='articlePage'>
                    { 
                        this.props.allArticles.map((article,index) => {
                            return (
                                <div key={index} className="articlePage__section-content-right-trending">
                                    <img className="articlePage__section-content-right-trending-image" alt="article" src={`${article[0].image}`}/>                                            
                                </div>
                            )
                        })
                    }
        </div>
    )
}
}


export default ArticlePage;

Upvotes: 0

Views: 137

Answers (2)

Hemadri Dasari
Hemadri Dasari

Reputation: 33994

You need to add conditional check before doing map. Here allArticles will be undefined initially. Also regarding key try to add the id from your data as a key when generating jsx elements in loop. if you don't have unique id generated for each object in the array data then use index as a key something like how I did below

 { 
    this.props.allArticles && this.props.allArticles.map((article,index) => {
          return (
               <div key={`Key${index}`} className="articlePage__section-content-right-trending">
                   <img className="articlePage__section-content-right-trending-image" alt="article" src={`${article[0].image}`}/>                                            
               </div>
          )
     })
 }

If you want to know the different ways of adding keys to jsx element you can refer here

Upvotes: 1

san
san

Reputation: 1693

In ArticlePage component, you can set defaultProps

class ArticlePage extends Component {
    static defaultProps = {
       allArticles: []
    }
    constructor(props) {
        super(props);
        this.state = {

        }
    }

render() {
    return (
        <div className='articlePage'>
                    { 
                        this.props.allArticles.map((article,index) => {
                            return (
                                <div key={index} className="articlePage__section-content-right-trending">
                                    <img className="articlePage__section-content-right-trending-image" alt="article" src={`${article[0].image}`}/>                                            
                                </div>
                            )
                        })
                    }
        </div>
    )
}
}


export default ArticlePage;

Upvotes: 1

Related Questions