【发布时间】:2020-09-19 07:17:43
【问题描述】:
- 我映射了一系列位置
- 我想通过onClick访问组件的key
- 这是我的控制台.log
const addTo = (element) => {console.log(element);};
return (
<>
{data.map((item) => {
return (
<Marker key={Math.random()} position={item.fields.location}>
<Popup>
{item.fields.name} <br></br>
<button
onClick={() => {
addTo(item.fields.name);
}}
>Add
</button>
</Popup>
</Marker>
);
})}
</>
);
}
【问题讨论】:
-
key of the component是什么意思? -
key是一个特殊的 react 属性。即使您将它作为道具传递,它也无法在组件中作为props.key访问。但是如果你需要它,你可以用不同的名字传递它:<MyComponent key={123} myKey={123} />并以props.myKey访问它
标签: reactjs next.js react-props react-component react-leaflet