[英]How to give a name to destructed object?
這是我的代碼:
const { tourId, title, startLocation,endLocation, details: {days}} = info;
以及我如何使用它
<BookingHeader tourId={tourId} title={title} startLocation={startLocation} endLocation={endLocation} days={days}/>
我想給被破壞的對象(bookingHeaderProps)起個名字,所以理想情況下我像這樣使用它
<BookingHeader {...bookingHeaderProps}/>
有人知道如何給被破壞的物體起名字嗎?
您很可能不需要名稱,只需使用“ {... this.props}”,但是如果確實要命名,則可以始終自己重建對象並將其作為prop傳遞。
當你解體時
const { a, b } = c;
您不會得到可以命名的“一個對象”。 你不是創建了兩個局部變量, a
和b
是現在分開 。 您可以再次將它們組合為組合(和命名)對象的鍵,如下所示:
const bookingHeaderProps = { tourId, title, startLocation, endLocation, days };
然后使用
<BookingHeader {...bookingHeaderProps} />
上面的步驟依賴於簡寫屬性名稱(ES2015) ,因此也必須予以支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.