자바개발자 리엑트 배우기 (hook)

Eungho won·2023년 5월 29일
0

React&JavaScript

목록 보기
4/6

In React, a Hook is a special function that lets you "hook into" React features. For example, state is a feature in React, and useState is a hook that allows you to add state to your functional components.

Before hooks were introduced in React 16.8, you couldn't use state and other React features unless you wrote a class component. But hooks allow you to use these features inside functional components, making them incredibly powerful.

Here are some examples of the most common hooks:

  • useState: This hook allows you to add React state to your functional components. It returns a state variable, and a function to update this state variable.

  • useEffect: This hook allows you to perform side effects in your components, such as data fetching, subscriptions, or manually changing the DOM. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

  • useContext: This hook allows you to access the value of your context without wrapping your component in a Context.Consumer component. It makes it easier to pass data through the component tree without having to pass props down manually at every level.

  • useReducer: This hook is usually preferable to useState when you have complex state logic that involves multiple sub-values. It also lets you optimize performance for components that trigger deep updates because you can pass dispatch down instead of callbacks.

  • useMemo and useCallback: These hooks help you optimize performance in your application by avoiding expensive calculations on every render and preventing unnecessary renders of child components, respectively.

You can also create your own custom hooks, which lets you extract component logic into reusable functions. A custom hook is a JavaScript function whose name starts with ”use” and that may call other hooks.

profile
kill to code

0개의 댓글