본문 바로가기
개념/React

React) local parameter - example 1

by kiseno 2025. 3. 19.
728x90
반응형
SMALL
import { Component } from 'react';

class example_0 extends Component{
    id = 1
    setId = (n) => {
        this.id = n;
    }
    printId = () => {
        console.log(this.id);
    }
    render(){
        return (
            <div>
                MyComponent
            </div>
        );
    }
}

export default example_0;

### Component Structure and Analysis

1. **Instance Variables**:
   ```jsx
   id = 1
   ```
   - The `id` is defined as an instance variable of the class with an initial value of `1`. Unlike state, changes to this variable will not trigger a re-render of the component. Instance variables are useful for storing information that doesn’t directly influence the view or needs to persist without causing updates each time it changes.

2. **Methods**:
   - **setId**:
     ```jsx
     setId = (n) => {
         this.id = n;
     }
     ```
     - This method is used to update the `id` instance variable. It takes a parameter `n` and sets `this.id` to `n`. Because `id` is an instance variable, changing it does not automatically cause the component to re-render, which means the UI will not update in response to this change.
   - **printId**:
     ```jsx
     printId = () => {
         console.log(this.id);
     }
     ```
     - This method simply logs the current value of `this.id` to the console. It’s an example of how you can access and use instance variables within class methods.

3. **Rendering**:
   ```jsx
   render(){
       return (
           <div>
               MyComponent
           </div>
       );
   }
   ```
   - The `render` method returns JSX that is rendered to the DOM. In this case, it's a simple `div` element containing the text "MyComponent". There's no interaction or display of the `id` value here, making this component static in its display.

### Discussion

This example is quite minimal and serves as a basic demonstration of handling and modifying non-state class properties in React. However, for a more illustrative example, you might want to include ways to interact with these methods (like buttons) and demonstrate how the component behaves in response to events:

### Enhanced Example with Interaction

Here's how you could enhance this component to include user interaction that triggers the methods:

```jsx
class EnhancedExample extends Component {
    id = 1

    setId = (n) => {
        this.id = n;
    }

    printId = () => {
        console.log(this.id);
    }

    render() {
        return (
            <div>
                <div>My Enhanced Component</div>
                <button onClick={() => this.setId(5)}>Set ID to 5</button>
                <button onClick={this.printId}>Print ID</button>
            </div>
        );
    }
}

export default EnhancedExample;
```

In this enhanced example:
- Added two buttons: one to change the `id` and another to print the `id` to the console.
- These additions demonstrate how instance variables and class methods can interact with user events without causing re-renders, unless explicitly managed or connected to the component's state.

### Conclusion

The original component you provided is a good start for understanding instance variables and methods in class components. Enhancing it with interactive elements can provide clearer insights into practical uses in real applications, particularly in scenarios where you need to manage information that doesn't directly affect the rendering logic.

728x90
반응형
LIST

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

React) custom Hook 1  (0) 2025.03.21
React) custom Hook - useInputs  (0) 2025.03.20
React) local parameter - example 2  (0) 2025.03.18
React) useReducer - counter  (0) 2025.03.17
React) useReducer - info  (0) 2025.03.16