Hi folks I'm trying to use flow with a custom useFetch hook. Here is the code:
import React, { useState, useEffect } from 'react'
function useFetch<FetchData>(
promiseFn: () => Promise<FetchData>,
args?: any[]
) {
const [loading, setLoading] = useState(false)
const [data, setData] = useState<?FetchData>()
const [error, setError] = useState<?Error>()
useEffect(() => {
setLoading(true)
promiseFn()
.then(setData)
.catch(setError)
.finally(() => setLoading(false))
})
return {
loading,
data,
error
}
}
function fetchKeys() {
return Promise.resolve([{ name: 'myKey' }])
}
type KeysData = {
name: string
}[]
function MyKeysPage() {
const { data, loading, error } = useFetch<KeysData>(fetchKeys)
if(!data) return 'Loading...'
return data.map(key => (<div>{key.name}</div>))
}
But it is throwing an error:
Cannot call
data.mapbecause propertymapis missing inFetchData[1].
You can take a better look here:
So I don't know how to fix that. Thanks for your help.