reactjs - 如何使用在渲染 React/Redux 中定义的变量作为回报?
问题描述
如何使用变量 sexname 作为回报?有错误“sexname
未定义”,如何解决?
render()
{
const { loading,popupVisible,edit } = this.state;
const{information, about, data, photo,}=this.props;
if(information.sex === 'w'){
let sexname = 'женский'
}else if(information.sex === 'm'){
let sexname = 'мужской'
}else {
let sexname ='Пока не указано';
}
return (
<Aboutinfo>{
information.sex !== null && information.sex !== undefined ?
sexname
: 'Пока не указано'
}</Aboutinfo>
);
}
解决方案
let
是阻塞范围的,因此它仅在直接块中可用(块是介于 之间的任何内容{ .... }
,在您的情况下是在if
语句中,我们可以通过将 移动let sexname
到函数顶部来解决这个问题。
render()
{
const { loading,popupVisible,edit } = this.state;
const{information, about, data, photo,}=this.props;
let sexname = ""
if(information.sex === 'w'){
sexname = 'женский'
}else if(information.sex === 'm'){
sexname = 'мужской'
}else {
sexname ='Пока не указано';
}
推荐阅读
- java - 我无法映射模型和 DTO 无法执行语句;SQL [n/a] 约束 [null]
- visual-studio-code - 如何让 VS Code 格式化空行?
- javascript - 在一个“.map”中为数组的每个元素获取多个对象
- java - Netbeans 12.3 包含带有错误标记的文件
- python - 将数据插入 sqlite3 表 Python 时出错
- r - 将R中的data.frame从宽格式重塑为长格式
- python - pyhon(kivy) 应用程序在每次更改后重新加载
- oracle - SQL Oracle - 如何使查询动态接受多天来计算到期
- solr - Solr 术语方面是否可以使用多个字段?
- .net - NUnit 和大量并行测试