I have 2 examples, one with class component and one with function component doing exactly the same thing. Im using react 16.13.1. I know that if you dont persist the event you will get an error saying that the event target is null. This happens as expected in class component. In function component though, this isnt the case. What is the difference between them?
export class App extends React.Component {
constructor() {
super();
this.state = { text: '' };
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
console.log(event.target.value);
this.setState(() => ({
text: event.target.value
}));
}
render() {
return (
<div>
<span>Text: </span>
<input onChange={this.handleChange} value={this.state.text} />
</div>
);
}
}
const App = () => {
const [state, setState] = useState({ text: "" });
const handleChange = (event) => {
console.log(event.target.value);
setState({
text: event.target.value,
});
};
return (
<div>
<span>Text: </span>
<input onChange={handleChange} value={state.text} />
</div>
);
};