Javascript 向下传递时在子组件中无法获得值
我试图将isOpen道具向下传递到Snackbar组件,但由于某种原因,当我在componentDidMount或componentDidUpdate中console.log this.props时,我看到的是this.props.message。如果有人能帮我解决这个问题,我将不胜感激,因为这个问题花费了我很多时间,但它看起来很简单。提前谢谢 父组件Javascript 向下传递时在子组件中无法获得值,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我试图将isOpen道具向下传递到Snackbar组件,但由于某种原因,当我在componentDidMount或componentDidUpdate中console.log this.props时,我看到的是this.props.message。如果有人能帮我解决这个问题,我将不胜感激,因为这个问题花费了我很多时间,但它看起来很简单。提前谢谢 父组件 从组件的React文档更新: 初始渲染时不调用此方法。 而且componentDidMount没有获得作为参数的道具,您必须使用this.pro
从
组件的React文档更新:
初始渲染时不调用此方法。
而且componentDidMount
没有获得作为参数的道具,您必须使用this.props
class SnackBar extends Component {
constructor (props) {
super(props)
this.state = {
open: false,
};
}
componentDidMount() {
console.log(this.props)
}
只是为了确保-您能将其改为isopen
而不是isopen
?您共享的代码看起来不错。试着在构造器内部打印你的道具。但是,如果它不能解决您的问题,请尝试传递isOpen的值,就像传递消息一样。i、 e.isOpen={this.state.openVal}其中openVal为true。尽管我认为这不是camelCase问题。它应该出现在componentDidMount@Upasana这里谢谢你的建议,我试过了,但不幸的是它不起作用。。。。您还有其他建议吗?@ErhunYilman使用上述共享代码,一切似乎都很好。如果你能在这里分享更多的代码会有帮助吗?一个简单的问题,你是使用material ui component Snackbar还是简单的react Snackbar?Contractor和componentDidMount
之间的道具没有任何变化,为什么会这样?啊,等等,小错误,检查我的编辑。不过,只需将其记录在构造函数中即可。
class SnackBar extends Component {
constructor (props) {
super(props)
this.state = {
open: false,
};
}
componentDidUpdate(prevProps,nextProps) {
console.log(this.props);
console.log(prevProps, nextProps);
}
class SnackBar extends Component {
constructor (props) {
super(props)
this.state = {
open: false,
};
}
componentDidMount() {
console.log(this.props)
}