相关文章推荐
讲道义的脸盆  ·  解析或序列化 XML - ...·  1 月前    · 
跑龙套的单杠  ·  分享6个对象数组去重的方法开发者社区·  1 月前    · 
纯真的炒饭  ·  MongoDB数据库中更新与删除数据 - ...·  1 月前    · 
体贴的红金鱼  ·  在 Office 加载项中使用 ...·  1 月前    · 
满身肌肉的八宝粥  ·  【Day31】ChatGPT請教教我:Jes ...·  2 周前    · 
打篮球的卤蛋  ·  上海大学研究生院·  6 月前    · 
踏实的墨镜  ·  elasticsearch实践篇:跨表joi ...·  1 年前    · 
打篮球的煎饼  ·  手工增加iPhone内存真的靠谱吗?影响质保 ...·  1 年前    · 
火爆的牛肉面  ·  RabbitMQ集群_51CTO博客_rab ...·  2 年前    · 
气宇轩昂的香菇  ·  在c#中textbox中填上x值,在char ...·  2 年前    · 
Code  ›  React技巧之具有空对象初始值的useState开发者社区
软件 string react const
https://cloud.tencent.com/developer/article/2077242
读研的葫芦
2 年前
作者头像
chuckQu
0 篇文章

React技巧之具有空对象初始值的useState

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 前端F2E > 正文

React技巧之具有空对象初始值的useState

发布 于 2022-08-19 15:18:10
594 0
举报

原文链接: https://bobbyhadz.com/blog/react-typescript-usestate-empty-object [1]

作者: Borislav Hadzhiev [2]

正文从这开始~

类型声明useState

要在React中用一个空对象的初始值来类型声明 useState 钩子,可以使用钩子泛型。比如说: const [employee, setEmployee] = useState<{[key: string]: any}>({}) 。 state 变量将被类型化为一个具有动态属性和值的对象。

// App.tsx
import {useEffect, useState} from 'react';
const App = () => {
  // 👇️ const employee: {[key: string]: any;}
  const [employee, setEmployee] = useState<{[key: string]: any}>({});
  useEffect(() => {
    setEmployee({
      name: 'Alice',
      salary: 100,
      department: 'Dev',
      tasks: ['dev', 'test', 'ship'],
  }, []);
  return (
      <h2>Name: {employee.name}</h2>
      <h2>Salary: {employee.salary}</h2>
export default App;

{[key: string]: any} 是 TypeScript 中的索引签名语法,当我们不清楚一个类型的所有属性名称和值的时候,就可以使用索引签名。

示例中的索引签名意味着,当一个对象的索引是 string 时,将返回类型为 any 的值。

当你事先不知道对象的所有属性时,你可以使用这种方法。

你可以尝试用一个索引签名来覆盖一个特定属性的类型。

// App.tsx
import {useEffect, useState} from 'react';
type Employee = {
  [key: string]: any;
  age?: number;
  tasks?: string[];
const App = () => {
  // 👇️ const employee: {[key: string]: any;}
  const [employee, setEmployee] = useState<Employee>({});
  useEffect(() => {
    setEmployee({
      name: 'Alice',
      salary: 100,
      department: 'Dev',
      tasks: ['dev', 'test', 'ship'],
  }, []);
  return (
      <h2>Name: {employee.name}</h2>
      <h2>Salary: {employee.salary}</h2>
export default App;

我们将 age 和 tasks 属性标记为可选,并明确的为它们指定了类型。可选属性既可以拥有 undefined 值,也可以拥有指定的类型。这就是为什么我们仍然能够将 state 对象初始化为空对象。

然而,为我们事先知道的属性提供类型是十分有用的,因为 age 和 tasks 属性只能被设置为指定的类型。

如果对象的属性可以是多个类型,那么就是用联合类型。

import {useEffect, useState} from 'react';
type Employee = {
  [key: string]: any;
  // 👇️ age is number OR string
  age?: number | string;
  tasks?: string[] | number[];
const App = () => {
  // 👇️ const employee: {[key: string]: any;}
  const [employee, setEmployee] = useState<Employee>({});
  useEffect(() => {
    setEmployee({
      name: 'Alice',
      salary: 100,
      department: 'Dev',
      tasks: ['dev', 'test', 'ship'],
  }, []);
  return (
      <h2>Name: {employee.name}</h2>
      <h2>Salary: {employee.salary}</h2>
 
推荐文章
讲道义的脸盆  ·  解析或序列化 XML - XML:可扩展标记语言 | MDN
1 月前
跑龙套的单杠  ·  分享6个对象数组去重的方法开发者社区
1 月前
纯真的炒饭  ·  MongoDB数据库中更新与删除数据 - 龙恩0707
1 月前
体贴的红金鱼  ·  在 Office 加载项中使用 Office 对话框 API - Office Add-ins | Microsoft Learn
1 月前
满身肌肉的八宝粥  ·  【Day31】ChatGPT請教教我:Jest 單元測試(下) - 完整語法&教學 - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天
2 周前
打篮球的卤蛋  ·  上海大学研究生院
6 月前
踏实的墨镜  ·  elasticsearch实践篇:跨表join查询_es 跨表查询-CSDN博客
1 年前
打篮球的煎饼  ·  手工增加iPhone内存真的靠谱吗?影响质保!-中国教育和科研计算机网CERNET
1 年前
火爆的牛肉面  ·  RabbitMQ集群_51CTO博客_rabbitmq镜像集群
2 年前
气宇轩昂的香菇  ·  在c#中textbox中填上x值,在chart图上对应位置显示光标或ToolTip (说明)_微软技术-CSDN问答
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号