Last active
June 7, 2018 13:13
-
-
Save msokk/aeac4344c4168c93b0a853c1ee672dc8 to your computer and use it in GitHub Desktop.
react-request with TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Fetch, FetchSFCProps } from 'react-request'; | |
interface User { | |
id: number; | |
name: string; | |
username: string; | |
email: string; | |
address: { | |
street: string; | |
suite: string; | |
city: string; | |
zipcode: string; | |
geo: { | |
lat: string; | |
lng: string; | |
}; | |
}; | |
phone: string; | |
website: string; | |
company: { | |
name: string; | |
catchPhrase: string; | |
bs: string; | |
}; | |
} | |
/** | |
* Get all user | |
*/ | |
const GetUsers = (props: FetchSFCProps<User[]>) => ( | |
<Fetch url="https://jsonplaceholder.typicode.com/users" {...props} /> | |
); | |
export default { | |
GetUsers, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Api from './api'; | |
render() { | |
return ( | |
<Api.GetUsers lazy> | |
{({ fetching, data, doFetch }) => { | |
if (fetching) return <h1>Loading...</h1>; | |
if (!data) return <Button onClick={_e => doFetch()}>Load users</Button>; | |
return data.map(u => <h4 key={u.id}>{u.name}</h4>); | |
}} | |
</Api.GetUsers> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment