[英]props is undefined inside React component
I'm trying to clone my Main component's prop to all it's children. 我正在尝试将Main组件的prop克隆给所有子组件。 I'm using cloneElement but I get an error stating "props is undefined".
我正在使用cloneElement,但收到一条错误消息,指出“ props is undefined”。
It seem's others are having the same issue ( https://github.com/wesbos/Learn-Redux-Starter-Files/issues/6 ) 似乎其他人也有同样的问题( https://github.com/wesbos/Learn-Redux-Starter-Files/issues/6 )
This is the code for my main component: 这是我主要组件的代码:
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;
Here are my dependency versions: 这是我的依赖版本:
"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"
}
You just have to give your cloned the props like this 您只需要给克隆的道具这样的东西
{React.cloneElement(this.props.children, this.props)}
Take into account that cloneElement is only working if you give ONE node to main children. 考虑到只有将一个节点赋予主要子节点,cloneElement才起作用。 If many do a loop like explained in the documentation
如果有很多人像文档中所述进行循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.