简体   繁体   English

如何清除我的输入值?

[英]How do I clear my input that has value in it?

I have an input that's populated with values returned from an API that's stored into Redux state: 我有一个输入,其中填充了从存储为Redux状态的API返回的值:

state = {
  popoverScenarioName: null,
}

scenarioNameChange = (e) => (
  this.setState({popoverScenarioName: e.target.value})
)

// ....

<StyledInput
  placeholder="Scenario Name"
  onChange={(e) => scenarioNameChange(e)}
  onFocus={(e) => e.target.select()}
  value={this.state.scenarioName || database.inputs.scenarioName}
/>

When I click into the input and hit backspace to clear the entire field, it always repopulates the value with database.inputs.scenarioName . 当我单击输入并按退格键以清除整个字段时,它将始终使用database.inputs.scenarioName重新填充值。

I've tried setting state to something like 我试图将状态设置为类似

state = {
  popoverScenarioName: null || this.props.database.inputs.scenarioName,
}

but that didn't seem to work neither. 但这似乎也不起作用。 My other guess would be to write a dispatch to change database.inputs.scenarioName directly? 我的另一个猜测是编写一个直接更改database.inputs.scenarioName的调度程序?

state = {
   popoverScenarioName: null,
}

scenarioNameChange = (e) => (
   this.setState({popoverScenarioName: e.target.value})
 )

 clickHandler = () => {
    //...doing something here
    setState({popoverScenarioName: null})
 }

<StyledInput
  placeholder="Scenario Name"
  onChange={(e) => scenarioNameChange(e)}
  onFocus={(e) => e.target.select()}
  value={this.state.popoverScenarioName}
 />

<button onClick={this.clickHandler}>add todo</button>

where is your click handler? 您的点击处理程序在哪里?
after click clear your form 单击后清除表格

this one going from redux state "this.props.database."? 这是从redux状态“ this.props.database”开始的吗?

我通过这个道具实现了它:

scenarioName={this.state.popoverScenarioName === null ? database.inputs.scenarioName : this.state.popoverScenarioName}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM