我试图将名称为“key”的参数传递给 React 组件,但它不起作用。如果我使用“keyy”而不是“key”那么它就可以工作。
在我看来,“key”是一个受限关键字,我不能将它用作参数名称。
这是真的吗?
这是我的例子:
render() {
<MyComponent key='apple'
keyy='pear'>
}
最佳答案
是的,没错,key 是一个受限关键字,不会作为 props 传播。
Keys serve as a hint to React but they don't get passed to your components. If you need the same value in your component, pass it explicitly as a prop with a different name:
const content = posts.map((post) =>
<Post
key={post.id}
id={post.id}
title={post.title} />
);
With the example above, the Post component can read props.id, but not props.key.
在 docs 中阅读更多内容
关于javascript - 将 "key"参数传递给 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41934077/