5.asyncAndRouter.md

异步和路由

  1. 异步函数 async
    在react使用异步,与正常我们写异步函数没什么大的区别.主要时在请求完成后, 我们使用hook把数据更新, 仅此而已。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    // 异步API
    const [pets, setPets] = useState([])


    async function requestPets() {
    const { animals } = await pet.animals({
    location,
    breed,
    type: animal
    })

    setPets(animals || [])
    }
  2. 路由基础概念.

  • 渲染锚点
  • 触发路由渲染的入口
  • 动态路由参数
    1
    2
    3
    const test = <Details path="/details/:id" />

    // 在jsx中我们通过props去访问这个路由参数