Skip to content

Commit

Permalink
Created README and LICENCE (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel authored Jul 14, 2022
1 parent 7d73085 commit bc9b294
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Aleksandras Sukelovic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
128 changes: 127 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,127 @@
"# react-suspended-query"
# react-suspended-query

## Inspiration

This package was inspired by [SWR](https://swr.vercel.app/).


## Installation

```bash
npm i react-suspended-query
```

## What is this

This is a react fetching library based on [React Suspense](https://reactjs.org/docs/react-api.html#reactsuspense).

## The problem

The classic way of fetching data in React looks like this:

```jsx
const SomeComponent = () => {
// Create state for data, error and loading
const [data, setData] = useState(null)
const [error, setError] = useState(null)
const [loading, setLoading] = useState(true)

// Create useEffect to fetch data
useEffect(() => {
fetch('https://some-url/data')
.then(setData)
.catch(setError)
.finally(() => setLoading(false))
}, [])

// Make conditional rendering
if (loading) {
return <Spinner />
}

if (error) {
return <ErrorMessage error={error} />
}

return <div>
{data}
</div>
}
```

As you can see, there is a lot of boilerplate code and it is hard to write components like that.

## The solution with Suspense

In React 18 you can use Suspense for data fetching, but be aware that it is still experimental feature.

New API look like this:

```jsx
const App = () => {

// Wrap your component with ErrorBoundary to handle error state and Suspense to handle loading state
// Now we can define the error state of our App in one place as well as loading state
return <CacheGroup.Provider>
<ErrorBoundary>
<Suspense fallback={<Spinner />}>
<SomeComponent />
</Suspense>
</ErrorBoundary>
<CacheGroup.Provider>
}

const SomeComponent = () => {
const data = useQuery('https://some-url/data', fetch)

return <div>
{data}
</div>
}
```

See [ErrorBoundary](https://reactjs.org/docs/error-boundaries.html#gatsby-focus-wrapper) and [Suspense](https://reactjs.org/docs/react-api.html#reactsuspense) from React documentation.

## Cache groups

react-suspended-query provides the possibility to define cache groups. By default, you must wrap your component with `CacheGroup.Provider`, which will cache all fetches in children components. In general, some fetches depend on variables and others are consistent. To handle these cases, you can create multiple cache groups in one App.

## API

### useQuery

#### Calls given fetcher and processes the query:
- When promise is pending - renders Suspense.
- When fetcher throws error - passes it up the component tree.
- When promise resolves - returns data.

#### Arguments:
1. `key` - a string or an array of values fetcher depends on.
2. `fetcher` - function, which accepts your `key` in arguments and returns promise.

#### Example usage:
```jsx
const data = useQuery(['https://some-url/data', id], (key, id) => fetch(`${key}/${id}`))
```

<br/>

### createCacheGroup

#### Used to create cache group provider. Returns CacheGroup.

#### Example usage:
```jsx
const CacheGroup = createCacheGroup()

const App = () => {
return <CacheGroup.Provider>
{/* JSX */}
</CacheGroup.Provider>
}
```
<br/>

## License

The MIT License.

0 comments on commit bc9b294

Please sign in to comment.