我目前正在AddProductPage為我的Web應用程序構建一個組件。它props從其父級那里收到一堆,AddProductContainer并且應該將其劃分props為頁面上呈現的許多較小的組件。例子:AddProductPage.jsfunction AddProductPage(props) { return( <React.Fragment> <Component1 propsA={props.propsA} propsB={props.propsB} /> <Component2 propsC={props.propsC} propsD={props.propsD} propsE={props.propsE} /> // And so on... </React.Fragment> );}我已經決定將整個props對象分成較小的對象,每個組件一個,這樣我就可以更清晰地看到代碼在頁面上呈現的內容。喜歡:function AddProductPage(props) { const comp1Props = { propsA: props.propsA, propsB: props.propsB } const comp2Props = { propsC: props.propsC, propsD: props.propsD, propsE: props.propsE } return( <React.Fragment> <Component1 {...comp1Props}/> // <--- Easier to see what will render <Component2 {...comp2Porps}/> // And so on... </React.Fragment> );}我目前正在考慮是否有可能將所有動態props分解(或其他方法)分解為單個變量,因此我可以編寫如下內容:function AddProductPage(props) { // Some code to destructure all props into single variables const comp1Props = { propsA, propsB }; const comp2Props = { propsC, propsD, propsE }; return( <React.Fragment> <Component1 {...comp1Props}/> // <--- Easier to see what will render <Component2 {...comp2Porps}/> // And so on... </React.Fragment> );}我怎樣才能做到這一點?編輯我想我對這個問題還不夠清楚。但是,我的意思是動態地分解所有內容,而無需知道props名稱或名稱props。是否可以?喜歡:const {...props} = {...props}; // This is pseudo code這樣,我認為我的代碼將盡可能地干凈。謝謝。
動態破壞道具?
慕容708150
2021-04-16 14:15:49