-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): remove Component in favor of useInstances
- Loading branch information
Showing
6 changed files
with
70 additions
and
45 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
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,27 @@ | ||
## useInstances | ||
|
||
A hook for wrapping up and automatically refreshing data. | ||
|
||
### example | ||
|
||
```typescript | ||
import { useInstances } from '@theniledev/react'; | ||
|
||
const SomeComponent = () => { | ||
// get `myEntity`, refresh every 4s, refetch data when the button is clicked | ||
const { refetch, data } = useInstances('myOrg', 'myEntity', { | ||
refershInterval: 4000, | ||
}); | ||
|
||
return ( | ||
<Button | ||
onClick={() => { | ||
// do some submit logic | ||
refetch(); | ||
}} | ||
> | ||
Create new | ||
</Button> | ||
); | ||
}; | ||
``` |
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,31 @@ | ||
import { useNile } from '../../../context'; | ||
import Queries, { useQuery } from '../../queries'; | ||
import { useInterval } from '../useInterval'; | ||
|
||
type Props = { | ||
useQuery?: typeof useQuery; | ||
refreshInterval?: number; | ||
}; | ||
|
||
export const useInstances = ( | ||
orgId: string, | ||
entityType: string, | ||
props?: Props | ||
) => { | ||
const { useQuery: customUseQuery, refreshInterval } = props ?? {}; | ||
const nile = useNile(); | ||
const useQueryHook = customUseQuery ?? useQuery; | ||
|
||
const { refetch, data, isFetching } = useQueryHook( | ||
Queries.ListInstances(orgId, entityType), | ||
() => | ||
nile.entities.listInstances({ | ||
type: entityType, | ||
org: orgId, | ||
}) | ||
); | ||
|
||
useInterval(refetch, refreshInterval); | ||
|
||
return { data, refetch, isFetching }; | ||
}; |