React v.18 ã§ä½æããã¢ããªã§ A props object containing a "key" prop is being spread into JSX
ã¨ãã Warning ãçºçãã
Warning: props object containing a "key" prop is being spread into JSX: let props = {key: someKey, label: ..., size: ..., className: ..., disabled: ..., data-tag-index: ..., tabIndex: ..., onDelete: ...}; <Component {...props} /> React keys must be passed directly to JSX without using spread: let props = {label: ..., size: ..., className: ..., disabled: ..., data-tag-index: ..., tabIndex: ..., onDelete: ...}; <Component key={someKey} {...props} />
key
ã¨ããããããã£ã¯ç¹å¥ãªã®ã§ã¹ãã¬ããæ§æã§æ¸¡ããªãããã«ãã¨ãããã¨ããã
key
ã¨ããããããã£ãå«ã¾ãã props 㯠key
ã ãã¯å¥éæãåºãå¿
è¦ããã
ä»å Warning ãçºçããã³ã³ãã¼ãã³ãã¯ã«ã¼ãã§å¼ã³åºãã¦ããããã§ã¯ãªãã Database ããåã£ã¦ãããã¼ã¿ã« key
ã¨ããããããã£ãåå¨ããã®ã§ãã®è¦åãçºçãã¦ããã
Warning ãçºçãã¦ããã³ã³ãã¼ãã³ãã®å¼ã³åºã
type ComponentDataType = { id: string; key: string; ... }; const App: FC = () => { const data: ComponentDataType[] = getComponentData(); return ( <ul> {data.map((item) => ( <li key={item.id}> <MyComponent {...item} /> </li> )} </ul> ); };
item
ã« key
ã¨ããååã®ããããã£ãåå¨ããã®ã§ã<MyComponent {...item} />
㧠A props object containing a "key" prop is being spread into JSX
ã®è¦åãçºçãã
å¥é key
ãåãåºãã¦æ¸¡ãã° OK
type ComponentDataType = { id: string; key: string; ... }; const App: FC = () => { const data: ComponentDataType[] = getComponentData(); return ( <ul> {data.map(({key, ...item}) => ( <li key={item.id}> <MyComponent {...item} /> </li> )} </ul> ); };
ã«ã¼ãã§ä½ãããåã«è¨å®ãã key
ã§ãªãã¦ãã key
ã¨ããååã® props ã¯åãåºãã¦æ¸¡ãå¿
è¦ããã
ã³ã³ãã¼ãã³ã㧠key
ãä¸è¦ãªã {key: _key, ...item}
ã¨ããã° OK
ã³ã³ãã¼ãã³ãã® props ã¨ã㦠key
ã¨ããååã®ããããã£ã使ã£ãããå¤ããåå¾ãããã¼ã¿ã« key
ã¨ããããããã£ãããã¨ãã® warning ã«åºä¼ãå¯è½æ§ãããããã§ã
ããã
[åè]