[英]React: object is not defined 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)} />
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(e) {
this.setState({ temperature: e.target.value });
}
render() {
return (
<fieldset>
<legend>Temprature of water in celsius</legend>
<input
value={this.state.temperature}
onChange={this.handleTempChange}
/>
<BoilingPeek celsius={parseFloat(this.state.temperature)} />
</fieldset>
);
}
}
ReactDOM.render(<TempCalculator/>,document.getElementById("root"))
谢谢,已解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.