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 { useMachine } from "@xstate/react"; | |
import async from "./async-machine"; | |
function noop() {} | |
function useAsync({ trigger, onSuccess, onError } = {}) { | |
const [current, send] = useMachine( | |
async.withContext({ | |
trigger: trigger || noop, | |
onSuccess: onSuccess || noop, |
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 async from "./async-machine"; | |
import { interpret } from "xstate"; | |
function noop() {} | |
// create a Provider/Consumer pair | |
const AsyncContext = React.createContext({}); | |
AsyncContext.displayName = "Async"; |
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 { Machine } from "xstate"; | |
function noop() {} | |
const async = Machine( | |
{ | |
initial: "idle", | |
context: { | |
trigger: noop, | |
onSuccess: noop, |
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 Service from '@ember/service'; | |
import { tracked } from '@glimmer/tracking'; | |
export default class DogDataService extends Service { | |
@tracked data: IDog = null; | |
@tracked status: Status = Status.loading; | |
@tracked error: Error = null; | |
constructor() { | |
super(...arguments); |
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
function App() { | |
return ( | |
<Router> | |
<div className="App"> | |
{/* The data provider component responsible | |
for fetching and managing the data for the child components. | |
This needs to be at the top level of our component tree.*/} | |
<DogDataProvider> | |
<Nav /> | |
<main className="w-full py-5 mx-auto text-center text-white md:py-20 max-w-screen-xl"> |
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
export function useDogProviderState() { | |
const context = React.useContext(DogDataProviderContext); | |
if (context === undefined) { | |
throw new Error('useDogProviderState must be used within DogDataProvider.'); | |
} | |
return context; | |
} |
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
interface State { | |
data: IDog; | |
status: Status; | |
error: Error; | |
} | |
const initState: State = { status: Status.loading, data: null, error: null }; | |
const DogDataProviderContext = React.createContext(undefined); | |
DogDataProviderContext.displayName = 'DogDataProvider'; |
NewerOlder