[英]How to destructuring a prop
我在我的React項目中遇到了一個linter錯誤,涉及到破壞props,state和context的分配 。 鏈接在這里 。
我不明白該如何破壞這個元素。
const { job_id } = props.match.params.job_id;
可能只是
const { job_id } = props.match.params;
請查看JavaScript 對象解構文檔以獲取更多詳細信息。
const o = {id: '5', name: 'Jon'}; const { id } = o; console.log(id);
如果需要將其分配給新的變量名稱,也可以執行以下操作:
const o = {id: '5', name: 'Jon'}; const { id: alternative } = o; console.log(alternative);
在銷毀分配的右側,應該有您要從中獲取屬性的對象。
const { job_id } = props.match.params;
考慮具有prop
和prop2
屬性的對象。
const obj = { prop: "something", prop2:"string of prop2" } const { prop } = obj; //get the prop key of the variable 'obj' console.log(prop); //something const { prop2 } = obj.prop2 //get the property 'prop2' from the string "string of prop2" which is undefined. console.log(prop2) //undefined
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.