07. Displaying Platform Icons
| One of the Game Object | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
如果要展示游戏上架平台图标,需要用到返回的 parent_platforms 属性。而 platforms 更加细化。比如 parent_platforms 是 PlayStation,那么 platforms 可能就包含 PS 4、PS 5。
| parent_platform | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
parent_platforms 是储存 platform 对象的列表,因此需要对 Game 的类型新增标注:
| useGame.ts | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
按 CTRL+T 可以搜索函数、类和组件等。
回到 GameCard.tsx 中,先尝试输出 parent_platforms 中的平台名称。
| GameCard.tsx | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
接下来便是使用 React Icons 库显示图标,npm i react-icons@4.7.1。
由于渲染图标的逻辑与 GameCard 定位不符,因此需要新建一个组件 PlatformIconList.tsx 用于专门负责渲染游戏平台的图标。
| PlatformIconList.tsx | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
索引签名(Index Signature)允许你定义对象属性的键和值的类型。
语法:
{ [key: KeyType]: ValueType }
以 const iconMap: { [key: string]: IconType } = {} 为例:
iconMap是一个对象,具有动态的键和值。{ [key: string]: IconType }是类型注解,表示iconMap对象的键是字符串类型,值是IconType类型。IconType是一个类型别名或者接口,代表某种图标类型。