0
votes

I am working on a hacker news clone I am trying to get the ids of the top stories from their api using axios in componentDidMount and then making another axios call to get the stories and push them in a state array but when I try to map over and render that array nothing shows up

class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      posts: []
    }
  }

  componentDidMount() {
    axios.get('https://hacker-news.firebaseio.com/v0/topstories.json')
      .then( result => {
        result.data.slice(0, 10).forEach(element => {
          axios.get('https://hacker-news.firebaseio.com/v0/item/' + element + '.json')
            .then( value => {
              this.state.posts.push(value)
            })
            .catch(err =>{
              console.log(err)
            })
        })
      })
      .catch(err => {
          console.log(err);
      })
  }


  render() {
    return (
    <div>
      <Header title="Hacker News" />
      {this.state.posts.map( (element, index) => <Post key={element.data.id} serialNum={index} postTitle={element.data.title} postVotes={element.data.score} postAuthor={element.data.by} />) }
    </div> 
    )
  }
}
3
Dont do this: this.state.posts.push(value) Instead, create an array and do this.setState({ posts: newArray }) - Rajesh
Yeah I tried that too still doesn't show up, the weird thing is that when I try to console.log(this.state.posts) the array correctly shows up in the console but nothing shows up in the render - ayushdubey

3 Answers

1
votes

Try setting the state like this:

 axios.get('https://hacker-news.firebaseio.com/v0/item/' + element + '.json')
            .then( value => {
              this.setState({
                posts: [value, ...this.state.posts]
              })
            })
            .catch(err =>{
              console.log(err)
            })
        })

This way you're using setState and appending every new value to the existing state.

0
votes

As stated in the comments, don't use push for set state. In your code when you make the second request you must change the setState method to spread out the new value.

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      posts: []
    }
  }

  componentDidMount() {
    axios.get('https://hacker-news.firebaseio.com/v0/topstories.json')
      .then( result => {
        result.data.slice(0, 10).forEach(element => {
          axios.get('https://hacker-news.firebaseio.com/v0/item/' + element + '.json')
            .then( value => {
               this.setState(prevState => ({posts: [ value.data, ...prevState.posts]}))
            })
            .catch(err =>{
                console.log("err");
                console.log(err);
            })
        })
      })
      .catch(err => {
          console.log(err);
      })
  }


  render() {
    return (
       <div>
          {this.state.posts && this.state.posts.map( (element, index) => 
             <div key={element.id}>
                {element.title}
             </div>
          )}
        </div> 
    );
  }
 }
0
votes

componentDidMount() is called after Render() only once. React doesn't know about the state changes unless you use setState().

componentDidMount() {
    axios.get('https://hacker-news.firebaseio.com/v0/topstories.json')
      .then( result => {
        result.data.slice(0, 10).forEach(element => {
          axios.get('https://hacker-news.firebaseio.com/v0/item/' + element + '.json')
            .then( value => {
              this.setState({posts: [value, ...this.state.posts]})
            })
           })
         })
        }

Use this.setState({posts : [value, ...this.state.posts]}) instead of this.state.posts.push(value). using ... (spread operator) appends the value to the original posts array.