4 ms·
Okay so if i have: const read = (id: string) => API.client.get<State>(`things/${id}`).then(response => { return response.data }) const [ readThing
by twic 5y ago
Okay so if i have:
const read = (id: string) => API.client.get<State>(`things/${id}`).then(response => {
return response.data
})
const [ readThingRequest, readThing ] = usePromise(read)
And i want to read two things from the server:
const first = readThing('alpha');
const second = readThing('beta');
Then both will update the same readThingRequest, right? So the information in it will vary according to which one returns first or something?
This feels like slightly the wrong scoping. Either let me call the wrapped function multiple times, and get multiple metadata objects:
const [firstRequest, first] = readThing('alpha');
const [secondRequest, second] = readThing('beta');
Or push the wrapping down to the calling of the function:
const [firstRequest, first] = usePromise(read, 'alpha');
const [secondRequest, second] = usePromise(read, 'beta');
- loh 5y agoYou can think of it very similarly to `useState`, where it returns a particular state and a function to update that state. With `useState`: const [ firstState, setFirstState ] = useState('alpha') const [ secondState, setSecondState ] = useState('beta') With `usePromise`: const [ firstPromiseState, readFirstThing ] = usePromise(read) const [ secondPromiseState, readSecondThing ] = usePromise(read) To initialize the promise state similar to `useState('alpha')`: const [ firstPromiseState, readFirstThing ] = usePromise(read, { status: 'resolved', value: 'alpha' }) const [ secondPromiseState, readSecondThing ] = usePromise(read, { status: 'resolved', value: 'beta' })