• h.yoshida @hirokiyoshida837 ·

    最後のところの三項演算子は多少は短く書ける

      const list = useSWR<Array<string> | undefined>(repoData.data ? ["searchUserRepository", user, language] : null, () => {
        return repoData.data ? repoData.data
          .filter(x => x.language === language)
          .map(x => x.name) : undefined;
      })
    
      const list = useSWR<Array<string> | undefined>(repoData.data ? ["searchUserRepository", user, language] : null, () => {
        return repoData.data && repoData.data
          .filter(x => x.language === language)
          .map(x => x.name);
      })
    Edited by h.yoshida
  • h.yoshida @hirokiyoshida837 ·

    無理やりPromise書いたら消せた。これ毎回書くのと、Responseのほうがundefined含むUnionになるの、どっちもややこしくはあると感じるな

    
    /**
     * userのGitHubリポジトリから、指定した言語のリポジトリ名だけをリストにして取得する。
     * @param user
     * @param language
     */
    export const useSearchUserRepository = (user: string, language: string): SWRResponse<Array<string>, any> => {
    
      // 1. REST APIから GitHubのuserのデータを取得
      const userBaseURL = `https://api.github.com/users/${user}`
      const userData = useSWR<GitHubUser>(userBaseURL);
    
      // 2. 1で取得したuserの情報をもとに、リポジトリのリストを取得
      const repoData = useSWR<Array<Repository>>(userData.data ? userData.data.repos_url : null);
    
      // 2で取得したリポジトリのリストから、指定したlanguageが設定されているリポジトリの名前だけを抜き出す。
      const list = useSWR<Array<string>>(repoData.data ? ["searchUserRepository", user, language] : null, () => {
        return repoData.data ? repoData.data
          .filter(x => x.language === language)
          .map(x => x.name): new Promise<Array<string>>(_=> _);
      })
    
      return list
    }
    
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment