ReasonJun

React : Hooks 본문

Frontend/React

React : Hooks

ReasonJun 2023. 6. 12. 00:45
728x90

Hooks are a feature introduced in React 16.8 that allows you to use state and other React features without writing a class. They provide a way to add stateful logic to functional components, making it easier to reuse and organize your code. Hooks are designed to enable better code reuse, composition, and readability in React applications.

 

Using Hooks:

import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
};

export default Counter;

Using Class:

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() {
    const { count } = this.state;

    return (
      <div>
        <p>Count: {count}</p>
        <button onClick={this.increment.bind(this)}>Increment</button>
      </div>
    );
  }
}

export default Counter;

 

There are several built-in hooks available in React, including:

 

  1. useState: useState is the most commonly used hook that allows functional components to manage state. It returns a state value and a function to update that value. You can define and update state within a functional component, eliminating the need for class components.
  2. useEffect: useEffect is used to perform side effects in functional components, such as fetching data, subscribing to events, or manipulating the DOM. It replaces the lifecycle methods in class components (e.g., componentDidMount, componentDidUpdate, componentWillUnmount).
  3. useContext: useContext allows you to access the value of a React context within a functional component. It provides a way to avoid prop drilling and access global or shared state throughout your component tree.
  4. useReducer: useReducer is an alternative to useState for managing more complex state logic. It accepts a reducer function and an initial state, similar to the concept of reducers in Redux. It is useful when state transitions involve multiple actions and have more complex logic.
  5. useCallback: useCallback is used to memoize a function and prevent unnecessary re-rendering of child components that rely on that function. It is useful when passing callbacks to child components to optimize performance.
  6. useMemo: useMemo is similar to useCallback but memoizes the result of a function call. It allows you to optimize expensive calculations by caching the result until the dependencies change.
  7. useRef: useRef returns a mutable ref object that can persist across component renders. It can be used to hold references to DOM elements, store mutable values, or create flags to track component state changes.
  8. Custom Hooks: Custom Hooks are a way to reuse stateful logic across different components. By extracting common logic into a custom hook, you can share that logic between different components without duplicating code.

Hooks promote a more functional programming style and allow you to write cleaner, more concise code. They help to address common issues such as component reuse, state management, and side effects. With hooks, you can build complex applications using functional components, making React code easier to read, write, and maintain.

 

It's important to note that hooks can only be used within functional components and cannot be used in class components. They are widely adopted in the React community and have become the recommended approach for writing React components.

728x90
Comments