[英]Why default value in the input is setting after type any button on the keyboard
I have an input in my app where I'm trying to set a default value using function and later send that value to the server.我在我的应用程序中有一个输入,我尝试使用函数设置默认值,然后将该值发送到服务器。 The value is set only when I click any button from the keyboard.
仅当我单击键盘上的任何按钮时才设置该值。 If I don't do that, value is empty and I can't send it to the server.
如果我不这样做,则 value 为空,我无法将其发送到服务器。 I want to set that default value without clicking anything.
我想在不单击任何内容的情况下设置该默认值。 Any idea?
任何的想法?
The input is:输入是:
<div>
<input
className="form-control"
name="frequency"
value={this.state.frequency}
onChange={this.handleConstFrequency}
placeholder={'30'}
/>
</div>
handleConstFrequency function: handleConstFrequency 函数:
handleConstFrequency = () => {
this.setState({ frequency: '30' })
}
You could use React Hooks and define your state and then update it.您可以使用 React Hooks 并定义您的状态,然后更新它。
import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
export default function App() {
const [frequency, setFrequency] = useState(20);
const handleConstFrequency = (e) => {
setFrequency("30"); // Or override with value e.target.value
};
return (
<>
<div>
<input
className="form-control"
name="frequency"
value={frequency}
onChange={handleConstFrequency}
placeholder={"20"}
/>
</div>
</>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.