react file input onchange code example
Example 1: input onchange react type file
import * as React from "react";
export class FileSelector extends React.Component<undefined, undefined>
{
constructor(props: any)
{
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange(selectorFiles: FileList)
{
console.log(selectorFiles);
}
render ()
{
return <div>
<input type="file" onChange={ (e) => this.handleChange(e.target.files) } />
</div>;
}
}
Example 2: react text input onchange
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) { this.setState({value: event.target.value}); }
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} /> </label>
<input type="submit" value="Submit" />
</form>
);
}
}
Example 3: react file input
<input type="file" />