Home HTML CSS JavaScript React Gamedatum.com
× Home HTML CSS JavaScript React Gamedatum.com

React Components


React components are the building blocks of a React application. A component can be thought of as a self-contained module that encapsulates UI logic and behavior. React components can be defined as functional components or class components. Functional components are simpler and easier to read, while class components offer more advanced features such as lifecycle methods and state management.

Here's an example of a functional component:

                     
                      
                                           
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

                                           
                                           
                

And here's an example of a class component:

                     
                      
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Increment
        </button>
      </div>
    );
  }
}