dcz*_*zii 12 reactjs material-ui
我有一个selectField,我想在它上面设置一个值.假设我输入它,当我点击一个按钮时,按钮会调用一个能重置文本字段值的函数吗?
<TextField hintText="Enter Name" floatingLabelText="Client Name" autoWidth={1} ref='name'/>
Run Code Online (Sandbox Code Playgroud)
Wit*_*ult 24
你可以这样做
export default class MyCustomeField extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 'Enter text',
};
}
handleChange = (event) => {
this.setState({
value: event.target.value,
});
};
handleClick = () => {
this.setState({
value:'',
});
};
render() {
return (
<div>
<TextField
value={this.state.value}
onChange={this.handleChange}
/>
<button onClick={this.handleClick}>Reset Text</button>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
人们认为正确的方法是在类似于那里接受的答案的场景中控制组件,但您也可以以这种粗暴且文化上不可接受的方式控制价值。
<TextField ref='name'/>
this.refs.name.getInputNode().value = 'some value, hooray'
Run Code Online (Sandbox Code Playgroud)
你当然可以像这样检索值
this.refs.name.getValue()
Run Code Online (Sandbox Code Playgroud)
而不是使用ref你应该使用inputRef
const MyComponent = () => {
let input;
return (
<form
onSubmit={e => {
e.preventDefault();
console.log(input.value);
}}>
<TextField
hintText="Enter Name"
floatingLabelText="Client Name"
autoWidth={1}
inputRef={node => {
input = node;
}}/>
</form>
)
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19091 次 |
| 最近记录: |