反应道具破坏销毁回报未定义

时间:2018-07-30 04:31:59

标签: reactjs react-native destructuring react-props

我在销毁道具时遇到问题,它在我不进行销毁时就定义了,但是在执行时它为空。.

 const { navigation, book } = this.props; 
 {console.log('book: '+JSON.stringify(book))}

当我想像这样访问它时,它会在其中带有volumeInfo对象的控制台对象:

 const { navigation, book: { volumeInfo: { title, description }} } = this.props; 
 {console.log(title, description)}

那么它不是,它的props2.volumeInfo.title为null 尝试使用标题&& console.log(title),但没有任何内容...

这是第一种情况的控制台日志:

 book: {"kind":"books#volume","id":"__JIDwAAQBAJ","etag":"6y5RWEIbrcY","selfLink":"https://www.googleapis.com/books/v1/volumes/__JIDwAAQBAJ","volumeInfo":{"title":"Make Me","authors":["Kaye Blue"],"publisher":"Kaye Blue","description":"<p><b><i>They might make a great team … if they don’t kill each other first.</i></b></p><p>Cree Manning loves everything about being an attorney…except her colleague, Aaron. He may be an actual prince, but he’s also used to coasting through life on his good looks and status. Aaron's hot enough to melt ice, but his arrogance and more than questionable work ethic drive her up a wall.</p><p>His Royal Highness, Aaron Sarda, is third in line to the throne…which means his role in the Kingdom of Medina is mostly ornamental. He hates feeling useless, and working with Cree has taught him that he hates being looked down on even more. Sure, she’s gorgeous, but she’s also rigid, overbearing, and utterly immune to his charm.</

2 个答案:

答案 0 :(得分:1)

尝试一下

 const { navigation } = this.props; 
 const { title, description } = this.props.book.volumeInfo;
 {console.log(title, description)}

答案 1 :(得分:0)

在赋值左侧的destructuring assignment中,它定义了要从源变量解包的值。

因此,在这种分配情况下,它不是dat的变量声明。

这就是为什么语法错误。 未捕获的ReferenceError:未定义volumeInfo

如果您想通过dat访问volumeInfo,请这样做。

title

volumeInfo

就您而言,

var { book:  volumeInfo } = props; 

var { book: {volumeInfo }} = props; 将直接正确打印值,而不是通过var { book: { volumeInfo: { title, description }} } = props;