0

I'm passing an object, into a component I ReactJS.

 <Route path="/products/:id" render={(props) =>{
      return (<Book {...props} books={this.props.products}/>)
    }} />

books is an array object, where I find the one, that matches with the id given in

I can successfully log the objects bassed in

 let books =(props.books[0]) //logs object succesfully

but when I try to access a prop I get undefined.

I tried, using a setTimeOut function, to wait a bit, where it gives the same error, but also logs to the console

  let books =(props.books[0]) 
setTimeout(function(){
    console.log(books.id) // logs then fails
}, 100)

Here is an image of it

how can I make it stop?

Output of console.log(props.books)

(4) [{…}, {…}, {…}, {…}]
0: {id: 1, title: "How to Learn JavaScript - Vol 1", info: "Study hard"}
1: {id: 2, title: "How to Learn ES6", info: "Complete all exercises :-)"}
2: {id: 3, title: "How to Learn React", info: "Complete all your CA's"}
3: {id: 4, title: "Learn React", info: "Don't drink beers, until Friday (after four)"}
length: 4

Output of console.log(props.books[0])

{id: 1, title: "How to Learn JavaScript - Vol 1", info: "Study hard"}


  <Route path="/products" render={() => <Product products={this.state.books}/>} />

//here this.state.books equals [
    { id: 1,title: "How to Learn JavaScript - Vol 1", info: "Study hard"},
    { id: 2,title: "How to Learn ES6", info: "Complete all exercises :-)"},
    { id: 3,title: "How to Learn React",info: "Complete all your CA's"},
    { id: 4,title: "Learn React", info: "Don't drink beers, until Friday (after four)"
    }
  ]
Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69

0 Answers0