gpt4 book ai didi

javascript - 将未定义类型的不受控输入更改为受控

转载 作者:行者123 更新时间:2023-12-03 04:14:43 25 4
gpt4 key购买 nike

我正在从官方文档中学习ReactJS,并尝试编写一个温度计算器来计算水在给定温度下是否会沸腾。 (more info)尽管以下代码按预期运行,但我收到警告

"proxyConsole.js:56 Warning: TemperatureInput is changing an uncontrolled input of type undefined to be controlled. Input elements should not switch from uncontrolled to controlled"

谁能告诉我哪里错了?非常感谢!

function BoilingVerdict(props) {
if (props.temperature >= 100) {
return <p>The water would boil</p>
} else {
return <p>The water would not boil</p>
}
}

class TemperatureInput extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}

handleChange(e) {
this.props.onTemperatureChange(e.target.value);
}

render() {
const temperature = this.props.temperature;
return (
<fieldset>
<legend>Please enter temperature in {Calculate.scaleNames[this.props.scale]}</legend>
<input value={temperature} onChange={this.handleChange} />
</fieldset>
)
}
}

class Calculate extends React.Component {
constructor(props) {
super(props);
this.state = {
temperature: "",
scale: "c"
}
this.handleCelciusChange = this.handleCelciusChange.bind(this);
this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
}
static scaleNames = {
c: "Celcius",
f: "Fahrenheit"
}

static toCelsius(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}

static toFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}

static tryConvert(temp, convert) {
const input = parseFloat(temp);
if (Number.isNaN(input)) { return; }
const output = convert(input);
const rounded = Math.round(output * 1000) / 1000;
return rounded.toString();
}

handleCelciusChange(temperature) {
this.setState({
scale: 'c',
temperature: temperature
})
}

handleFahrenheitChange(temperature) {
this.setState({
scale: 'f',
temperature: temperature
})
}

render() {
const scale = this.state.scale;
const temperature = this.state.temperature;
const Celcius = scale === 'f' ? Calculate.tryConvert(temperature, Calculate.toCelsius) : temperature;
const Fahrenheit = scale === "c" ? Calculate.tryConvert(temperature, Calculate.toFahrenheit) : temperature;

return (
<div>
<TemperatureInput scale="c" onTemperatureChange={this.handleCelciusChange} temperature={Celcius} />
<TemperatureInput scale="f" onTemperatureChange={this.handleFahrenheitChange} temperature={Fahrenheit} />
<BoilingVerdict temperature={Celcius} />
</div>
)
}
}

ReactDOM.render(<Calculate />, document.getElementById('root'))

最佳答案

我认为问题出在 tryConvert 函数上。

在初始状态下,温度以空字符串("")启动。因此,当将此空字符串提供给 tryConvert 方法的 parseFloat 时,它会转换为 NaN。然后,您陷入 Number.isNaN 条件,并且您的方法返回 undefined

稍后,当您在字段中输入值时,该值将更改为真正的 float ,并且输入从不受控制(未定义值)切换到受控(有关受控和非受控状态的更多信息,请参阅此文档https://facebook.github.io/react/docs/forms.html#controlled-components)

您有多种方法可以修复代码。您可以修复 tryConvert 以始终返回有效数字(即使用户输入随机字符串),也可以仅修复您所在州的 temperence 属性的初始值。

希望有帮助:)

关于javascript - 将未定义类型的不受控输入更改为受控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44189423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com