2 April 2020

onChange event in react js

  • Create Components folder under src folder
  • Add ReactEvents.js file and add below Code
import React, { Component } from "react";
class ReactEvents extends Component {
constructor() {
super();
this.state = {
Name: ""
};
}
changeText = event => {
this.setState({
Name: event.target.value
});
};
render() {
return (
<div>
React Events Exp Name:
<input type="text" id="Name" onChange={this.changeText} />
<div>{this.state.Name}</div>
</div>
);
}
}
export default ReactEvents;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import ReactEvents from "./components/ReactEvents";

function App() {
return (
<div className="App">
<ReactEvents />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:


No comments:

Post a Comment