Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
TypeError: no se pueden leer las propiedades de undefined (leyendo 'id') en React

Creé un sitio web pero encuentro un problema cuando presiono el botón "Cargar más" después de 3 veces. Me da un error de "Error de tipo: no se pueden leer las propiedades de undefined (leyendo 'id')"... ¡Supongo que hay algo mal con la función de división y mapa! PD ¡Intenté llamar "id" a otro componente pero fue en vano!

 class PostsList extends React.Component { constructor(props) { super(props); this.state = { posts: [], visible: 3, error: false }; this.loadMore = this.loadMore.bind(this); } loadMore() { this.setState((prev) => { return {visible: prev.visible + 3}; }); } componentDidMount() { fetch("https://jsonplaceholder.typicode.com/posts").then( res => res.json() ).then(res => { this.setState({ posts: res }); }).catch(error => { console.error(error); this.setState({ error: true }); }); } render() { const { classes } = this.props; return ( <div className={classes.section}> <h2 className={classes.title}>Latest news</h2> <div> <GridContainer> { this.state.posts.slice(0, this.state.visible).map((post, i) => { return ( <PostCard key={i} id={posts[i].id} date={posts[i].date} fbimg={posts[i].fbimg} description={posts[i].description} fbpost={posts[i].fbpost} /> ); }) } </GridContainer> <GridContainer> <GridItem> {this.state.visible < this.state.posts.length && <Button color="info" onClick={this.loadMore} > Load more </Button> } </GridItem> </GridContainer> </div> </div> ); } }

¡¡¡Gracias de antemano!!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

las posts no están definidas dentro de su función de representación. ¿Quiso decir this.state.posts ?

Además, no hay necesidad de usar el índice para acceder a las publicaciones cuando ya tiene la post única disponible desde su función de map . Así que cambia posts[i].id a post.id .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!