如何在 React 中使用“this.props.params"? [英] How to use 'this.props.params' in React?
本文介绍了如何在 React 中使用“this.props.params"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我向我的组件发送了一个特定的 id (idValue) 并像这样使用它:
I send a certain id (idValue) to my component and use it like this:
componentDidMount() {
this.onHandleFetch(this.props.params.idValue);
}
但有时这个组件在没有任何输入参数(没有 idValue)的情况下打开,我得到这个错误:
But sometimes this component opens without any input parameters (without idValue) and I got this error:
TypeError: Cannot read property 'idValue' of undefined
我该如何解决?
我曾尝试使用typeof,但没有成功
I've tried to use typeof, but it was unsuccessful
在传递值的组件中,我使用了context router
In the component from which the value is transferred, I use context router
推荐答案
static defaultProps = {
params:{}
}
这篇关于如何在 React 中使用“this.props.params"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文