首页 > 解决方案 > 如何使用 Ignite Bowser 2 Boilerplate 在 React Native 中点击/使用帖子并获取 api。(Mobx 状态树,输入脚本)

问题描述

我是 React Native 的新手,请提供一些 Github 链接或您自己的代码以供参考。将我视为 RN 的初学者。

我发现对 RN、Mobx 状态树、Ignite 等的开放支持非常少,所以不仅仅是发布和获取 API 参考,如果您发现与上述主题相关的任何有用信息,请随时分享。

提前致谢。

标签: javascripttypescriptreact-nativereduxmobx-state-tree

解决方案


Mobx 状态树,使用 Ignite Bowler,您将拥有 api.ts 文件,您可以在其中指定 API 调用。

 async getUser(userToken: string): Promise<Types.GetUserResult> {
// make the api call

const response: ApiResponse<any> = await this.apisauce.post(`api/v1/sales/login?authCode=${userToken}`)

if (!response.ok) {
  const problem = getGeneralApiProblem(response)
  if (problem) return problem
}


// transform the data into the format we are expecting
try {
  try {
    const rawUser = response.data
    console.log('rawUser'+ rawUser)
    const user: UserSnapshot = convertRawUserToUserStore(rawUser)
    return { kind: "ok", user }
    console.log({ user })
  } catch (e) {
    __DEV__ && console.tron.log(e.message)
    return { kind: "bad-data" }
  }
} catch {
  return { kind: "bad-data" }
}
}

考虑一下,我们将从这个 API 调用中获取用户数据,您可以注意到有属于 User Model 的 UserSnapshot,Snapshot 会自动保存数据,您不需要 Aysnc 存储来保存或检索数据。


推荐阅读