我试图将我的主要组件克隆给所有孩子。我使用了cloneElement,但是我收到错误声明"道具未定义"。
似乎其他人也有同样的问题(https://github.com/wesbos/Learn-Redux-Starter-Files/issues/6)
这是我的主要组件的代码:
import React from 'react';
import Nav from './Nav';
const Main = React.createClass({
render() {
return (
<div className="wrapper">
<Nav />
{React.cloneElement(this.props.children, { ...this.props, children: props.children.props.children })}
</div>
)
}
});
export default Main;
以下是我的依赖版本:
"devDependencies": {
"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-react-display-name": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"express": "^4.13.4",
"node-sass": "^3.4.2",
"react-transform-catch-errors": "^1.0.2",
"react-transform-hmr": "^1.0.4",
"redbox-react": "^1.2.6",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1",
"webpack-dev-middleware": "^1.6.1",
"webpack-hot-middleware": "^2.10.0"
},
"dependencies": {
"bulma": "0.0.18",
"classnames": "^2.2.3",
"react": "^15.1.0",
"react-addons-css-transition-group": "^15.0.1",
"react-dom": "^15.1.0",
"react-redux": "^4.4.5",
"react-router": "^2.4.1",
"react-router-redux": "^4.0.4",
"redux": "^3.5.2",
"shuffle-array": "^1.0.0"
}
答案 0 :(得分:0)
你只需要克隆这样的道具
{React.cloneElement(this.props.children, this.props)}
考虑到只有将一个节点提供给主要子节点时,cloneElement才有效。如果许多人像documentation
中所解释的那样进行循环