reactjs - React 中的 componentWillReceiveProps 问题
问题描述
如果我使用下面的代码,我会收到错误。
componentWillReceiveProps(nextProps) {
if (nextProps.uploadImage.message === 'File uploaded') {
this.setState({ photostatus: 'image' });
}
else {
this.setState({ photostatus: 'input' });
}
}
但是如果我使用下面的代码,我不会收到错误
componentWillReceiveProps(nextProps) {
if (nextProps.uploadImage) {
this.setState({ photostatus: 'image' });
}
else {
this.setState({ photostatus: 'input' });
}
}
我的错误如下
解决方案
你得到一个错误,因为你试图找到一个message
属性的值undefined
。
你必须改变你的 if 条件是这样的,
componentWillReceiveProps(nextProps) {
if (nextProps.uploadImage && (nextProps.uploadImage.message === 'File uploaded')) {
this.setState({ photostatus: 'image' });
}
else {
this.setState({ photostatus: 'input' });
}
}
推荐阅读
- ssl - 计算引擎:出现 SSL 错误:例程:CONNECT_CR_SRVR_HELLO:错误的版本号
- r - R不会在数据框中读取CSV的数值
- amazon-web-services - api 网关找不到 lambda 别名
- postgresql - PostgreSQL 更新触发器:关系“新”不存在
- git - 如何将 gpg 密钥传输到另一台计算机?
- formatting - 仅突出显示已“拖”过来的文本/代码?
- react-native - 无法创建自定义 StackNavigator - “无法注册导航器”?
- r - 试图获取从列值派生的 checkboxGroupInput 以过滤条形图,但不断收到各种错误?
- javascript - 如何添加文档并将其 ID 立即存储在 Firebase 的其他地方?使用本机反应
- javascript - 运行 react-app-rewired build 时忽略文件