@@ -8,14 +8,15 @@ This package includes different tools that support you with common tasks.
88- [ Usage] ( #usage )
99- [ Build & Publish] ( #build--publish )
1010 - [ useInterval] ( #useinterval )
11+ - [ Async Data] ( #async-data )
1112- [ Information] ( #information )
1213
1314## Usage
1415
1516First, you have to install the package:
1617
1718``` bash
18- npm install @aboutbits/react-tooling
19+ npm install @aboutbits/react-toolbox
1920```
2021
2122Second, you can make use of the different tools.
@@ -29,9 +30,9 @@ The hook takes two parameters:
2930- ` callback ` : The callback function that should be executed.
3031- ` delay ` : The delay in milliseconds or null, if the interval should be paused.
3132
32- ``` jsx
33+ ``` tsx
3334import React , { useState } from ' react'
34- import { useInterval } from ' @aboutbits/react-tooling '
35+ import { useInterval } from ' @aboutbits/react-toolbox '
3536
3637const MyCommponent = () => {
3738 const [step, setStep] = useState (10 )
@@ -44,6 +45,72 @@ const MyCommponent = () => {
4445}
4546```
4647
48+ ### Async Data
49+
50+ This part includes a utility component, that can be used to render loading, success and error views based on async state.
51+
52+ ``` tsx
53+ import React , { useEffect } from ' react'
54+ import { AsyncView } from ' @aboutbits/react-toolbox'
55+
56+ type Data = {
57+ greeting: string
58+ }
59+
60+ type Error = {
61+ message: string
62+ }
63+
64+ const MyCommponent = () => {
65+ const [data, setData] = useState <Data | undefined >()
66+ const [error, setError] = useState <Error | undefined >()
67+
68+ useEffect (() => {
69+ fetch (' https://jsonplaceholder.typicode.com/todos/1' )
70+ .then (response => setData (response .json ()))
71+ .catch (error => setError (error ))
72+ })
73+
74+ return (
75+ <AsyncView
76+ data = { data }
77+ error = { error }
78+ renderLoading = { <div >Loading</div >}
79+ renderSuccess = { (data ) => <div >{ data .greeting } </div >}
80+ renderError = { (error ) => <div >{ error .message } </div >} />
81+ );
82+ }
83+ ```
84+
85+ And using SWR:
86+
87+ ``` tsx
88+ import React , { useEffect } from ' react'
89+ import { useSWR } from ' swr'
90+ import { AsyncView } from ' @aboutbits/react-toolbox'
91+
92+ type Data = {
93+ greeting: string
94+ }
95+
96+ type Error = {
97+ message: string
98+ }
99+
100+ const MyCommponent = () => {
101+ const { data, error } = useSWR (' https://jsonplaceholder.typicode.com/todos/1' )
102+
103+ return (
104+ <AsyncView
105+ data = { data }
106+ error = { error }
107+ renderLoading = { ' Loading' }
108+ renderSuccess = { ' Success' }
109+ renderError = { ' Error' } />
110+ );
111+ }
112+ ```
113+
47114## Build & Publish
48115
49116To publish the package commit all changes and push them to main. Then run one of the following commands locally:
@@ -64,6 +131,7 @@ For support, please contact [info@aboutbits.it](mailto:info@aboutbits.it).
64131
65132### Credits
66133
134+ - [ Martin Malfertheiner] ( https://github.com/mmalfertheiner )
67135- [ Alex Lanz] ( https://github.com/alexlanz )
68136- [ All Contributors] ( ../../contributors )
69137
0 commit comments