【发布时间】:2018-09-18 19:00:06
【问题描述】:
我正在学习“清晰”的 ReactJS(即它不是 JSX)。这是我的简单代码:
(() => {
class List extends React.Component{
constructor({tag, attributes, items}){
super({tag, attributes, items})
this.tag = tag
this.attributes = attributes
this.items = items
}
render(){
return React.createElement(this.props.tag, this.props.attributes, items.map(
(n, i) => new React.createElement(ListItem,
{ attributes: { ...n.attributes, key: i }, value: n.value })))
}
}
class ListItem extends React.Component{
constructor({attributes, value}){
super({attributes, value})
this.attributes = attributes
this.value = value
}
render(){
return React.createElement("li", this.props.attributes, this.props.value)
}
}
const items = [
{ attributes: null, value: "A" },
{ attributes: null, value: "B" },
{ attributes: null, value: "C" },
{ attributes: null, value: "D" },
{ attributes: null, value: "E" }
]
const root = document.getElementById("root")
ReactDOM.render(new React.createElement(List, { tag: "ul", attributes: null, items }), root)
})()
请注意:我在super 中放入了与组件构造函数中相同的参数。我也将key 用于li 元素。但我得到这样的错误:
警告:List(...): 在
List中调用 super() 时,请确保通过 与传递组件的构造函数相同的道具。警告:数组或迭代器中的每个子元素都应该有一个唯一的“键” 支柱。
检查
List的渲染方法。在ListItem中(由List创建)
在列表中
警告:ListItem(...):在
ListItem中调用 super() 时,请确保 传递与组件的构造函数相同的道具 通过了。警告:ListItem(...):在ListItem中调用 super() 时, 确保传递与组件的构造函数相同的道具 通过了。警告:ListItem(...):在调用 super() 时ListItem,确保传递与您的组件相同的道具 构造函数被传递。警告:ListItem(...):调用 super() 时 在ListItem中,确保传递与您的相同的道具 组件的构造函数已通过。警告:ListItem(...):当 在ListItem中调用 super(),确保传递相同的道具 您的组件的构造函数已通过。
我做错了什么?
【问题讨论】:
标签: javascript reactjs