Skip to content

Commit aafd563

Browse files
lalli-floresakoushke
authored andcommitted
feat(hooks): add usePerson react hook
1 parent b1660b6 commit aafd563

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/components/hooks/usePerson.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {useState, useEffect} from 'react';
2+
3+
/**
4+
* Custom hook that returns person data of the given ID.
5+
*
6+
* @param {string} personID ID of the person for which to return data.
7+
* @param {PeopleAdapter} adapter Component data adapter from which to retrieve data.
8+
* @returns {Person} Data of the person
9+
*/
10+
export default function usePerson(personID, adapter) {
11+
const [person, setPerson] = useState(undefined);
12+
13+
useEffect(() => {
14+
// eslint-disable-next-line no-console
15+
const onError = (error) => console.error(error.message);
16+
const subscription = adapter.getPerson(personID).subscribe(setPerson, onError);
17+
18+
return () => {
19+
subscription.unsubscribe();
20+
};
21+
}, [person]);
22+
23+
return person;
24+
}

0 commit comments

Comments
 (0)