Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Typing Lists and Keys | Section
TypeScript for React Development

bookTyping Lists and Keys

Deslize para mostrar o menu

In React, lists are used to render multiple items. In TypeScript, you define the type of items inside the list, so each item has a clear structure.

Typing a List of Values

const items: string[] = ["Apple", "Banana", "Orange"];

Rendering a list:

items.map((item) => <li key={item}>{item}</li>);
  • Each item is a string;
  • Key helps React track elements.

Typing a List of Objects

type User = {
  id: number;
  name: string;
};

const users: User[] = [
  { id: 1, name: "Alex" },
  { id: 2, name: "John" },
];

Rendering object list:

users.map((user) => (
  <li key={user.id}>{user.name}</li>
));
  • Each item follows the User type;
  • Use a unique key (like id).

Define the type of list items, and use a unique key when rendering them.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 28

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 28
some-alt