最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 如何在TypeScript中使用React Hook useState

    正文概述 掘金(未定义变量)   2021-03-22   499

    前言:

    TypeScript给JavaScript和ReactJS生态系统带来了巨大的变化。它提高了开发效率,TypeScript的项目往往更加可靠,以及在开发过程中更容易排查bug。这篇文章总结了一些我在学习TypeScript中,如何使用useState,并声明state类型。

    useState源码

    首先,让我们来看看React源码中的useState是如何声明的。

    // ...
    /**
     * Returns a stateful value, and a function to update it.
     *
     * @version 16.8.0
     * @see https://reactjs.org/docs/hooks-reference.html#usestate
     */
    function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
    // convenience overload when first argument is ommitted
    /**
     * Returns a stateful value, and a function to update it.
     *
     * @version 16.8.0
     * @see https://reactjs.org/docs/hooks-reference.html#usestate
     */
    function useState<S = undefined>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>];
    // ...
    

    在React源码中,useState有两个函数。第二个函数重写(override)了第一个函数,使我们可以在调用useState时不直接声明state变量类型。

    值得注意的是,第一个方法接一个名为S的TypeScript泛型,通过它,我们可以定义state的变量类型。

    useState基础用法

    以下是在TypeScript中使用useState的基本例子。

    import React, {useState} from 'react'
    
    export default function App() {
      const [name, setName] = useState<string>('未定义变量')
      const [age, setAge] = useState<number>(28)
      const [isProgrammer, setIsProgrammer] = useState<boolean>(true)
    
      return (
        <div>
          <ul>
            <li>Name: {name}</li>
            <li>Age: {age}</li>
            <li>Programmer: {isProgrammer ? 'Yes' : 'No'}</li>
          </ul>
        </div>
      )
    }
    

    如果你在set函数中的参数不符合声明的变量类型,程序会报错。

    import React, {useEffect, useState} from 'react'
    
    export default function App() {
      const [name, setName] = useState<string>('未定义变量')
      const [age, setAge] = useState<number>(28)
      const [isProgrammer, setIsProgrammer] = useState<boolean>(true)
    
      useEffect(() => {
        // Error: Argument of type '28' is not assignable to parameter of type 'SetStateAction<string>'.ts(2345)
        setName(28)
        // Error: Argument of type 'true' is not assignable to parameter of type 'SetStateAction<number>'.ts(2345)
        setAge(true)
        // Error: Argument of type '"Gabriel Rufino"' is not assignable to parameter of type 'SetStateAction<boolean>'.
        setIsProgrammer('未定义变量')
      }, [])
    
      return (
        <div>
          <ul>
            <li>Name: {name}</li>
            <li>Age: {age}</li>
            <li>Programmer: {isProgrammer ? 'Yes' : 'No'}</li>
          </ul>
        </div>
      )
    }
    

    但是对于JavaScript原始类型,我们其实不需要声明变量类型,因为TypeScript可以自己判断相应的变量类型。

    import React, {useEffect, useState} from 'react'
    
    export default function App() {
      const [name, setName] = useState('未定义变量')
      const [age, setAge] = useState(28)
      const [isProgrammer, setIsProgrammer] = useState(true)
    
      return (
        <div>
          <ul>
            <li>Name: {name}</li>
            <li>Age: {age}</li>
            <li>Programmer: {isProgrammer ? 'Yes' : 'No'}</li>
          </ul>
        </div>
      )
    }
    

    useState进阶用法

    当我们需要使用useState处理复杂数据时(如数组或者对象),我们需要借用TypeScript中接口(interface)这个概念。

    假设我们需要在useState中声明如下数据。

    [
      {
        "id": 1,
        "name": "蔡文姬",
        "type": "辅助"
      },
      {
        "id": 1,
        "name": "后裔",
        "type": "射手"
      },
      {
        "id": 1,
        "name": "鲁班7号",
        "type": "射手"
      }
    ]
    

    我们需要首先定义一个代表这组数据类型的接口(interface)。

    interface IChampion {
      id: number;
      name: string;
      type: string;
    }
    

    然后我们在使用useState中,声明我们的state为Champion类型变量。

    import React, {useState} from 'react'
    
    interface IChampion {
      id: number;
      name: string;
      type: string;
    }
    
    export default function Champions() {
      const [champions, setChampions] = useState<IChampion[]>([
        {
          id: 1,
          name: '蔡文姬',
          type: '辅助'
        },
        {
          id: 1,
          name: '后裔',
          type: '射手'
        },
        {
          id: 1,
          name: '鲁班7号',
          type: '射手'
        }
      ])
    
      return (
        <div>
          <ul>
            {champions.map(champion=> (
              <li key={champion.id}>{champion.name} - {champion.type}</li>
            ))}
          </ul>
        </div>
      )
    }
    

    但通常,我们会从API中异步获取数据,而非在useState中直接声明变量。

    import React, {useState, useEffect} from 'react'
    import axios from 'axios'
    
    interface IChampion {
      id: number;
      name: string;
      type: string;
    }
    
    export default function Champions() {
      const [champions, setChampions] = useState<IChampion[]>([])
    
      useEffect(() => {
        axios.get<IUser[]>('https://api.yourservice.com/champions')
          .then(({ data }) => {
            setChampions(data)
          })
      }, [])
    
      return (
        <div>
          <ul>
            {champions.map((champion: IChampion) => (
              <li key={champion.id}>{champion.name} - {champion.type}</li>
            ))}
          </ul>
        </div>
      )
    }
    

    以上就是在React TypeScript,useState的一些用法。


    --阅读更多文章,请关注我的公众号:未定义变量


    起源地下载网 » 如何在TypeScript中使用React Hook useState

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元