0
我正在使用反应,并且遇到了问题。反应 - 将道具传递给道具上的组件
我有一个组件需要接受另一个组件作为道具,并传入相同的组件其他道具。 例子:
export default class Item extends React.Component {
render() {
return (<div onClick={this.props.onClick}>Some content.</div>)
}
}
Item.propTypes = {
onClick: PropTypes.func.isRequired
}
export default class Container extends React.Component {
onClick() {
// Do something.
}
render() {
// render here the item and passing it my onClick method.
}
}
Container.propTypes = {
item: PropTypes.element.isRequired
}
编辑: 所以我大概可以使用ES5语法,做这样的事情:
React.createElement(item, {
onClick: this.onClick
});
但我怎么能做到这一点的ES6?
你真的能做到吗?我的意思是你传递了一个实际的元素在<>中听起来不错 – Pachu
是的,你可以。这是jsx组件.. – anoop