Javascript 如何删除TextField状态
我有一个文本字段。如果状态为null,但文本字段值显示在文本字段中Javascript 如何删除TextField状态,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我有一个文本字段。如果状态为null,但文本字段值显示在文本字段中 <TextField style={{ width: '65%'}} id="standard-search" label="Residential Address" type="text"
<TextField
style={{ width: '65%'}}
id="standard-search"
label="Residential Address"
type="text"
margin="dense"
variant="outlined"
name="ResidentialAddress"
onChange={(e)=>this.handleInputChange(e)}
/>
this.handleInputChange(e)}
/>
像这样定义您的值value={this.state.input\u value}
<TextField
style={{ width: '65%'}}
id="standard-search"
label="Residential Address"
type="text"
margin="dense"
variant="outlined"
name="ResidentialAddress"
onChange={(e)=>this.handleInputChange(e)}
value={this.state.input_value}
/>
this.handleInputChange(e)}
value={this.state.input_value}
/>
您需要将文本框值
绑定到状态属性。根据您使用的组件类型,您可以尝试以下选项
对于类组件:
class YouComponentName extends Component {
constructor (props, context) {
super(props, context)
this.state = {
yourTextBoxValue: ""
}
}
handleInputChange (event) {
this.setState({
yourTextBoxValue: event.target.value;
});
}
render(){
<>
<TextField
style={{ width: '65%'}}
id="standard-search"
label="Residential Address"
type="text"
margin="dense"
variant="outlined"
name="ResidentialAddress"
onChange={(e)=>this.handleInputChange(e)}
value = {this.state.yourTextBoxValue}
/>
</>
}
类YouComponentName扩展组件{
构造函数(道具、上下文){
超级(道具、背景)
此.state={
yourTextBoxValue:“
}
}
handleInputChange(事件){
这是我的国家({
yourTextBoxValue:event.target.value;
});
}
render(){
this.handleInputChange(e)}
value={this.state.yourTextBoxValue}
/>
}
对于功能组件(使用React挂钩):
函数组件名称(props){
const[yourTextBoxValue,setYourTextBoxValue]=useState(“”)
常量handleInputChange=(事件)=>{
这是我的国家({
设置YourTextBoxValue(event.target.value);
});
}
返回(
)
}
检查此项,如果对您有帮助,请接受/投票。我还有一项question@ZulqurnainHuda是的,告诉我?{this.setState({values:event.target.value})localStorage.setItem(“currentLocation”,JSON.stringify(event.target.value))this.props.updateCurrentLocation(event.target.value)}}margin=“normal”>本地存储数据可用,但未显示在选定区域,但单击“显示选定区域”
function YourComponentName(props) {
const [yourTextBoxValue, setYourTextBoxValue] = useState("")
const handleInputChange = (event) => {
this.setState({
setYourTextBoxValue(event.target.value);
});
}
return (
<>
<TextField
style={{ width: '65%'}}
id="standard-search"
label="Residential Address"
type="text"
margin="dense"
variant="outlined"
name="ResidentialAddress"
onChange={handleInputChange}
value = {yourTextBoxValue}
/>
</>
)
}