React is a JavaScript library developed by Facebook for building user interfaces (UIs) and managing the state of UI components. It allows you to create reusable UI components and efficiently update them when data changes. React follows a declarative approach, where you describe what your UI should look like at any given moment, and React takes care of updating the actual UI.
npx create-react-app my-react-app
cd my-react-app
import React from 'react'; function FunctionalComponent() { return ( <div> <h1>Hello from Functional Component!</h1> </div> ); } export default FunctionalComponent;
import React, { Component } from 'react';
class ClassComponent extends Component {
render() {
return (
<div>
<h1>Hello from Class Component!</h1>
</div>
);
}
}
export default ClassComponent;
import React from 'react'; import './App.css'; import FunctionalComponent from './FunctionalComponent'; import ClassComponent from './ClassComponent'; function App() { return ( <div className="App"> <FunctionalComponent /> <ClassComponent /> </div> ); } export default App;
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
increment = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
render() {
return (
<div>
<h2>Counter: {this.state.count}</h2>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
export default Counter;
import React from 'react'; import './App.css'; import Counter from './Counter'; function App() { return ( <div className="App"> <Counter /> </div> ); } export default App;