【问题标题】:Why the input doesnt work when generated by React为什么输入在由 React 生成时不起作用
【发布时间】:2021-10-09 05:02:44
【问题描述】:

当我将它添加到index.html 时,此输入正确。但是当由反应生成时它不起作用。输入形式的数字只是不改变。为什么会这样?

<input type="date" name="date" value="2003-12-16" max="2021-07-03"/>

【问题讨论】:

  • 那是因为你使用了value,使其不受控制。所以它现在不受 React 控制。您应该使用defaultValue 属性而不是value
  • 在这里阅读:itnext.io/…

标签: javascript node.js reactjs typescript


【解决方案1】:

为了实现您的目标,您必须使用 React 方式 (Controlled Components):

<input type="date" value={this.state.value} onChange={this.handleChange} max="2021-07-03" />

工作演示:Codesandbox

要了解Uncontrolled Components,请访问:Uncontrolled Components

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 2022-01-23
    • 1970-01-01
    • 2017-04-19
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多