javascript - 反应“不能破坏财产”
问题描述
所以我正在尝试学习 React,我正在学习一个教程,我跟着教程做所有事情,这个人保存了更改并编译,但我的给出了这个错误:
TypeError: Cannot destructure property 'text' of 'seasonConfig[season]' as it is undefined.
这是我要渲染的组件:
import React from 'react';
const seasonConfig = {
summer: {
text: '',
iconName: 'sun'
},
winter: {
text: '',
iconName: 'snowflake'
}
};
const getSeason = (lat, month) => {
if (month > 2 && month < 9) {
return lat > 0 ? 'summer ' : 'winter';
} else {
return lat > 0 ? 'winter ' : 'summer';
}
};
const SeasonDisplay = (props) => {
const season = getSeason(props.lat, new Date().getMonth());
const { text, iconName } = seasonConfig[season];
return (
<div>
<i className={` massive ${iconName} icon`} />
<h1>{text}</h1>
<i className={` massive ${iconName} icon`} />
</div>
);
};
export default SeasonDisplay;
解决方案
在 if 条件下删除夏季和冬季后的空间:
const getSeason = (lat, month) => {
if (month > 2 && month < 9) {
return lat > 0 ? 'summer' : 'winter'; // 'summer ' => 'summer'
} else {
return lat > 0 ? 'winter' : 'summer'; // 'winter ' => 'winter'
}
};
或者,如果我们真的需要空间,也可以这样做seasonConfig[season.trim()]
。
推荐阅读
- c++ - How to properly pass a string through multiple child constructors to parent. C++
- swift - CocoaPods 的 Xcode 10.1 Swift 编译器错误
- php - Getting no user on update request
- processing - Creating an array of "PrintWriter" in processing?
- sql-server - 当 Sqoop-Import 没有尝试连接到 SQL Server 时,Sqoop-Job 命令失败
- python - 我在 python3 中得到 TabError 但在 python2 中没有
- animation - 如何在 didUpdateWidget 上更改颤动动画的持续时间?
- scala - Spark To Cassandra:向 Cassandra 写入没有空值的稀疏行
- google-apps-script - 谷歌表格自定义功能 - 如果单元格不为空,如何从范围中获取单元格位置
- python-3.x - 我正在尝试创建一个输入字符串并返回字符串中位于字符串中奇数位置的元音数量的函数