-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow passing a callback to useFetch's
run
to modify init (#76)
* allow passing a callback to useFetch's `run` to modify init * useFetch: allow passing an object to `run` to be spread over `init` * better typings for `run`, add example * Upgrade storybook. * run() now returns void. Fixed code style. * Improve readability of parseResponse function. * Improve readability of isDefer. * Avoid nested ternaries. * Code style. * Extend docs on 'run'.
- Loading branch information
1 parent
dda874a
commit bca0187
Showing
8 changed files
with
184 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from "react" | ||
import { useFetch } from "react-async" | ||
|
||
export function FetchHookExample() { | ||
const result = useFetch<{ token: string }>("https://reqres.in/api/login", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
}) | ||
const { run } = result | ||
|
||
return ( | ||
<> | ||
<h2>with fetch hook:</h2> | ||
<button onClick={run}>just run it without login data</button> | ||
<button | ||
onClick={() => | ||
run(init => ({ | ||
...init, | ||
body: JSON.stringify({ | ||
email: "eve.holt@reqres.in", | ||
password: "cityslicka", | ||
}), | ||
})) | ||
} | ||
> | ||
run it with valid login data (init callback) | ||
</button> | ||
<button | ||
onClick={() => | ||
run({ | ||
body: JSON.stringify({ | ||
email: "eve.holt@reqres.in", | ||
password: "cityslicka", | ||
}), | ||
}) | ||
} | ||
> | ||
run it with valid login data (init object) | ||
</button> | ||
<br /> | ||
Status: | ||
<br /> | ||
{result.isInitial && "initial"} | ||
{result.isLoading && "loading"} | ||
{result.isRejected && "rejected"} | ||
{result.isResolved && `token: ${result.data.token}`} | ||
</> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | ||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | ||
sans-serif; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", | ||
"Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
monospace; | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace; | ||
} |
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
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
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
Oops, something went wrong.