【问题标题】:React-javascript : My react input isn't working. It becomes immutable once i set the ValueReact-javascript:我的反应输入不起作用。一旦我设置了值,它就变得不可变
【发布时间】:2020-12-05 17:46:28
【问题描述】:

一旦我将值设置为我的状态组件,我的反应输入组件就变得不可编辑

state = {text: ''}



return(
                   <input id="search" 
                   type="text"
                   value={this.state.text}
                    onChange={e => this.setState = ({text: e.target.value })}/>
)

一旦我注释掉它的值,它就可以正常工作。

【问题讨论】:

  • this.setState = ({text: e.target.value } 应该是this.setState({text: e.target.value }

标签: javascript reactjs input


【解决方案1】:

你错误地使用了 setState,setState 是一个函数,它需要传入参数来更新状态,你不能给函数赋值。

下面是一个可以解决您的问题的示例。

state = {text: ''}

return(
         <input id="search" 
          type="text"
          value={this.state.text}
          onChange={e => this.setState({text: e.target.value })}/>
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2020-04-09
    • 2014-07-07
    • 2017-05-09
    • 2019-05-13
    • 2017-05-11
    • 2015-05-05
    相关资源
    最近更新 更多