본문 바로가기
개념/React

React) Event basic

by kiseno 2025. 3. 31.
728x90
반응형
SMALL
import { Component } from 'react';
class EventPractice extends Component{
    render(){
        return(
            <div>
                <h1> practice Event </h1>
            </div>
        );
    }
}

export default EventPractice;

### Component Overview

1. **Importing Component from React**:
   ```jsx
   import { Component } from 'react';
   ```
   This line imports the `Component` class from the React library, which is necessary for creating class components in React.

2. **Class Component Definition**:
   ```jsx
   class EventPractice extends Component {
   ```
   - `EventPractice` is defined as a class that extends `Component`. This setup means `EventPractice` inherits features from React's `Component` class, making it a React component.

3. **Rendering Method**:
   ```jsx
   render() {
       return (
           <div>
               <h1>practice Event</h1>
           </div>
       );
   }
   ```
   - The `render` method is a required method in class components that outputs JSX. In this case, it returns a `div` element containing an `h1` tag with the text "practice Event".
   - This `render` method dictates what the DOM will display for this component—just a simple message, indicating the component's name or purpose.

4. **Exporting the Component**:
   ```jsx
   export default EventPractice;
   ```
   - This line exports the `EventPractice` component as the default export of the file, making it available for import in other parts of your application.

### Current State and Potential Enhancements

As it stands, the component does not include any state, props, or event handlers, which are common in React components for handling data or user interactions. Here are some enhancements you might consider to add more functionality:

- **Adding State**: You can introduce state to manage data within the component. For instance, you could add an input field to allow users to type a message and store this message in the component's state.

- **Handling Events**: To make the component interactive, consider adding event handlers. For example, you could add a button that, when clicked, triggers an action (like displaying an alert with a message typed by the user).

- **Using Props**: If this component is intended to be used within a larger application, you might want to make it more reusable by accepting props. This would allow you to customize the component based on where it's used.

Here is a simple example that includes an input field and a button to demonstrate adding state and handling events:
```jsx
class EventPractice extends Component {
    state = {
        message: ''
    };

    handleChange = (e) => {
        this.setState({ message: e.target.value });
    };

    handleClick = () => {
        alert(this.state.message);
        this.setState({ message: '' }); // Clear the message post-alert
    };

    render() {
        return (
            <div>
                <h1>Practice Event</h1>
                <input
                    type="text"
                    value={this.state.message}
                    onChange={this.handleChange}
                    placeholder="Enter a message"
                />
                <button onClick={this.handleClick}>Show Message</button>
            </div>
        );
    }
}

export default EventPractice;
```
This enhancement allows the component to interact with the user by accepting input and responding to a button click.

728x90
반응형
LIST

'개념 > React' 카테고리의 다른 글

React) this setState function parameter  (0) 2025.04.02
Reat) click the button print out comment value set empty  (0) 2025.04.01
React) Event Practice 1  (0) 2025.03.30
React) Event Practice 2  (0) 2025.03.29
React) function style of Component  (0) 2025.03.28