[英]Parent Component isn't passing prop to child Component
So in my Parent component's render function I have: 因此,在我的Parent组件的render函数中,我有:
const BasicDetails = React.createClass({
propTypes: {
showFxMargin: bool
},
getDefaultProps() {
return {
showFxMargin: false
};
},
render() {
return(
// other stuff...
console.log('inside BasicDetails.jsx showFxMargin = ' + showFxMargin);
<FxMargin
showFxMargin={showFxMargin}
/>);
}
});
and in the child component I have: 在子组件中,我有:
import _ from 'lodash';
import React from 'react';
const FxMargin = React.createClass({
render() {
const {
showFxMargin
} = this.props;
console.log('inside FxMargin.jsx showFxMargin = ' + showFxMargin);
return showFxMargin ? (
<div>
<h1>FX Margin Baby</h1>
</div>
) : (
<p></p>
);
}
});
export default FxMargin;
In my console I'm getting: 在我的控制台中,我得到:
inside BasicDetails.jsx showFxMargin = true
and inside BasicDetails.jsx showFxMargin = false 并在BasicDetails.jsx showFxMargin = false中
But I never get: 但是我从来没有得到:
inside FxMargin.jsx showFxMargin = true
How am I passing the prop incorrectly? 我如何错误地传递道具? I'm not getting any errors
我没有任何错误
Your render()
function inside of your BasicDetails
component contains a console.log
statement inside the return. 您的
BasicDetails
组件内部的render()
函数在返回值内部包含console.log
语句。 If this is the exact code that you're trying to use, then that is why your components aren't rendering correctly. 如果这是您要使用的确切代码,那么这就是组件无法正确呈现的原因。
With that line removed, your code works just fine: http://jsbin.com/jozewoxubu/edit?js,output 删除该行后,您的代码就可以正常工作: http : //jsbin.com/jozewoxubu/edit?js,output
make the render method of BasicDetails
component either - 使
BasicDetails
组件的render方法-
render() {
const {showFxMargin} = this.props; //change
console.log('inside BasicDetails.jsx showFxMargin = ' + showFxMargin);
return(
// other stuff...
<FxMargin
showFxMargin={showFxMargin}
/>);
}
Or - 要么 -
render() {
console.log('inside BasicDetails.jsx showFxMargin = ' + this.props.showFxMargin); //change
return(
// other stuff...
<FxMargin
showFxMargin={this.props.showFxMargin} //change
/>);
}
Hope this solves! 希望这能解决!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.