Setting Up React and TypeScript Flashcards
What does React.ReactNode allow as children in a React component?
It allows JSX elements and strings.
How do you define a type for props in React with TypeScript?
Use type Props = { type?: string; heading: string; children: React.ReactNode; closable?: boolean; onClose?: () => void; }
.
How can you ensure type safety when passing props in React components?
Define a Props type and use it when declaring the component function.
How can you define a state type explicitly in React with TypeScript?
Use a generic argument, e.g., const [visible, setVisible] = useState<boolean>();
.
What is the purpose of React DevTools’ Profiler panel?
It analyzes performance and detects slow components during interactions.
Why should useState types be explicitly defined in some cases?
useState
may not always infer types correctly, so explicit types ensure type safety.