Reusable Components Flashcards

1
Q

What is the purpose of generic type parameters in TypeScript functions?

A

They allow flexibility by enabling the function to work with different data types, defined within angled brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are generic types defined in TypeScript?

A

By using a type alias with generic parameters in angled brackets, e.g., type TypeName<T1, T2> = { ... };.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define generic props for a React component?

A

Use generic type parameters in the props definition, e.g., type Props<Item> = { items: Item[]; };.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you make a React component reusable with generics?

A

By using generic props within the component, e.g., export function List<Item>({ items }: Props<Item>) { ... }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is props spreading in React?

A

It is the process of passing all the props of an HTML element to a component, using the rest parameter and spreading them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does ComponentPropsWithoutRef do in React?

A

It references the props of an HTML element (e.g., ul), allowing you to pass them as props to a component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

It references the props of an HTML element (e.g., ul), allowing you to pass them as props to a component.

A

Render props allow a prop (other than children) to be a function that renders elements, making components more reusable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When would you use render props in React?

A

When the children prop is already in use and you want to pass a function for rendering elements, often for data-driven lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Provide an example of using render props in React.

A

<List data={[...]} renderItem={(item) => <div>{item.text}</div>} />

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are custom hooks in React?

A

Functions that start with use, return an object structure, and can accept parameters or use other React hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why are custom hooks useful in React?

A

They isolate logic, making it reusable across components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do custom hooks help with code reuse?

A

By encapsulating reusable logic, such as state management or side effects, and returning values in an object format.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the naming convention for custom hooks in React?

A

Custom hooks must start with the prefix use, helping ESLint identify them as hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Provide an example of a custom hook in React.

A
export function useToggle() {
  const [toggleValue, setToggleValue] = useState(false);
  function toggle() {
    setToggleValue(!toggleValue);
  }
  return { toggleValue, toggle };
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a common use case for render props in React?

A

Data-driven lists where the rendering of list items can be customized by passing a function to the renderItem prop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly