반응형
React fragment에 주요 소품을 추가할 수 있습니까?
생성 중dl
반응:
<dl>
{
highlights.map(highlight => {
const count = text.split(highlight).length - 1;
return (
<>
<dt key={`dt-${highlight.id}`}>{highlight}</dt>
<dd key={`dd-${highlight.id}`}>{count}</dd>
</>
);
})
}
</dl>
경고 메시지가 나타납니다.
경고:목록의 각 아이들은 고유한 "키" 받침대를 가지고 있어야 합니다.
경고는 삭제되지만 원하는 HTML은 생성되지 않습니다.
<dl>
{
highlights.map(highlight => {
const count = text.split(highlight).length - 1;
return (
<div key={highlight.id}>
<dt>{highlight}</dt>
<dd>{count}</dd>
</div>
);
})
}
</dl>
그리고 나는 그것을 추가할 수 없다.key
파편을 떠받치다<> </>
).
어떻게 이 문제를 해결할 수 있을까요?
리액트 사용 중16.12.0
.
fragment에 키를 추가하려면 full fragment 구문을 사용해야 합니다.
<React.Fragment key={your key}>
...
</React.Fragment>
https://reactjs.org/docs/fragments.html#keyed-fragments 에서 문서를 참조해 주세요.
네, fragment의 단축 버전에서는 불가능한 fragment(</>)의 키를 아래 형식으로 추가할 수 있습니다.
<Fragment key={your key}></Fragment>
이 방법을 사용하여 구성 요소 목록에 키를 자동으로 할당할 수도 있습니다.
React.Children.toArray(someData.map(data=><div>{data.name}</div>))
언급URL : https://stackoverflow.com/questions/59390955/can-i-add-a-key-prop-to-a-react-fragment
반응형
'programing' 카테고리의 다른 글
AJAX 요구의 신뢰성을 확인하는 방법 (0) | 2023.02.13 |
---|---|
유형 스크립트의 트리거 클릭 - 유형 'Element'에 속성 'click'이 없습니다. (0) | 2023.02.13 |
TypeScript의 "declare class"와 "interface"의 차이점은 무엇입니까? (0) | 2023.02.13 |
MBean [HikariDataSource(HikariPool-0)]을(를) 키 '데이터소스'에 등록할 수 없습니다. (0) | 2023.02.13 |
문자열을 JSON 개체로 변환 (0) | 2023.02.09 |