米脂
2022-10-21 11:02:19
你可以用這個檢查!!dataJson && Object.keys(dataJson).lengthfunction check(dataJson) { return !!dataJson && Object.keys(dataJson).length;}console.log(check(""));console.log(check([]));console.log(check({}));console.log(check({"name" :"test"}));
3 回答

翻過高山走不出你
TA貢獻1875條經驗 獲得超3個贊
React expects stable static key which will last till component life
Using this key, it decide whether new component instance need to
create or have to update existing component instance property.
react-uuid每當您調用時都會生成動態密鑰,uuid()并且它將在每次渲染時重新生成。
每當發生重新渲染時,動態生成的新密鑰將導致創建一個新的組件實例。
最好只從集合中創建一個密鑰,該密鑰不會更改并存在于集合中,直到數據存在于集合中。
盡量避免react-uuid并使用第一種方法。

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
我不會使用索引作為鍵,因為
當索引用作鍵時,重新排序列表或從列表中添加和刪除項目可能會導致組件狀態出現問題。如果鍵是索引,則重新排序項目會更改它。因此,組件狀態可能會混淆,并可能將舊密鑰用于不同的組件實例。
因此,請避免這種做法,并確保生成唯一的 ID 以分配為鍵。
因此,正如您提到的,UUid 是最好的。唯一生成密鑰的來源
希望能幫助到你。隨時懷疑
添加回答
舉報
0/150
提交
取消