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

What are Fragments in React?

Câu trả lời

React Fragments are a feature in React that allows developers to group multiple JSX elements without adding extra nodes to the Document Object Model (DOM). This capability is particularly useful when you need to return multiple elements from a component's render method but do not want to add an additional wrapping element like a <div> to the DOM, which can interfere with the desired layout or styling.

Key Characteristics of React Fragments

  1. No Additional DOM Node: Fragments do not contribute any actual elements to the DOM. This means they allow you to wrap elements in a component's return function without creating an extra DOM node[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15].

  2. Syntax Options: React Fragments can be declared in two ways:

    • Using the <React.Fragment> tag[2][3][4][5][6][7][8][9][10][11][12][13][14][15].
    • Using the shorthand syntax <>...</>[2][3][4][5][6][7][8][9][10][11][12][13][14][15]. However, the shorthand syntax does not support keys or attributes.
  3. Support for Keys: When rendering a list of items, each item in a list may need a unique identifier known as a 'key'. React Fragments support the use of keys when using the explicit <React.Fragment> syntax, which is necessary for React's reconciliation process during updates[2][3][4][5][6][7][8][9][10][11][12][13][14][15].

Practical Use Cases

  • Tables: Fragments are particularly useful in scenarios like rendering multiple table rows or cells within a single table component without wrapping them in an unnecessary DOM element, which would be semantically incorrect and could break the table layout[2][3][4][5][6][7][8][9][10][11][12][13][14][15].

  • Lists: When mapping over an ar...

junior

junior

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

senior

What's the difference between useCallback and useMemo in practice?What's the difference between useCallback and useMemo in practice?

middle

What's the typical pattern for rendering a list of components from an array in React?

junior

What are Controlled components in ReactJS?

Bình luận

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

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