ReactJS - How to set value for text field from material-ui?

I have selectField and I want to set a value on it. Say I click on it, and when I click on the button, does the button call a function that will reset the value of the text field?

<TextField hintText="Enter Name" floatingLabelText="Client Name" autoWidth={1} ref='name'/> 
+5
source share
2 answers

You can do it this way

 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> ); } } 
+12
source

He argued that the correct way is to control the component in the script, such as the accepted answer, but you can also control the value in this rude and culturally unacceptable way.

 <TextField ref='name'/> this.refs.name.getInputNode().value = 'some value, hooray' 

and you can of course get that value

 this.refs.name.getValue() 
+3
source

Source: https://habr.com/ru/post/1244413/


All Articles