Câu hỏi phỏng vấn Reactjs
Câu hỏi

What's the difference between useRef and createRef ?

Câu trả lời

The difference between useRef and createRef in React lies primarily in their usage within different types of components and their behavior across renders.

useRef is a hook that is used within functional components. It was introduced in React version 16.8 as part of the Hooks API. When you use useRef, it creates a mutable ref object that persists for the lifetime of the component. This means that the ref object retains its value across re-renders of the component, without creating a new instance each time. This is useful for storing a reference to a DOM element or for keeping any mutable value that does not trigger a re-render when updated[1][2][3].

On the other hand, createRef is a method that is typically used within class components. It creates a new ref object each time the component re-renders. This means that if you use createRef in a functional component, a new ref object will be created on every render, which can lead to inconsistencies and is generally not the desired behavior[1][3].

In summary, useRef is used in functional components and maintains the sam...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What are the advantages of Batching in ReactJS?

expert

Can a custom React hook return JSX?

middle

What would be the common mistake of function being called every time the component renders?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào