javascript - React:对象未定义 no-undef
问题描述
我制作了组件TempCalculator,它将计算水是否会在给定温度下沸腾。此外,它为当前输入值呈现 BoilingVerdict。
const BoilingPeek = (props) => {
return props.celsius >= 100 ? (
<p>Water would boil.</p>
) : (
<p>Water is would not boil.</p>
);
};
class TempCalculator extends React.Component {
constructor(props) {
super(props);
this.handleTempChange = this.handleTempChange.bind(this);
this.state = {
temperature: "",
};
}
handleTempChange(event) {
this.setState({ temperature: event.target.name });
}
render() {
return (
<fieldset>
<legend>Temprature of water in celsius</legend>
<input name={temperature} onChange={this.handleTempChange} />
<BoilingPeek celsius={parseFloat(temperature)} />
</fieldset>
);
}
}
ReactDOM.render(<TempCalculator/>,document.getElementById("root"))
错误
'temperature' is not defined no-undef
解决方案
那是因为temperature
是一个状态。像这样使用:
<input name={this.state.temperature} onChange={this.handleTempChange} />
<BoilingPeek celsius={parseFloat(this.state.temperature)} />
推荐阅读
- c# - InvalidOperationException:尝试激活时无法解析“Data.IRestaurantsData”类型的服务
- sql-server-2008 - 动态选择语句将记录集转换为可读格式
- vue.js - Vue:数据中的每个对象都是未定义的
- python - 熊猫图相关性
- python - 如何使用 tkinter 和 worker 函数通过队列在多处理中共享数据
- php - PHP中基于正则表达式分析日志文件
- flutter - 如何摆脱“不正确使用 ParentDataWidget”错误?
- r - 从密度ggplot中提取峰值
- python - 在 Python 对话框中绘制图形
- wordpress - 课程状态模板问题(课程单元状态)