-
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.
started website and created initial pages
- Loading branch information
1 parent
3518f29
commit 5a2cf2c
Showing
11 changed files
with
19,829 additions
and
56,324 deletions.
There are no files selected for viewing
36,622 changes: 0 additions & 36,622 deletions
36,622
tests/react-providerx-test-app/package-lock.json
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,17 @@ | ||
--- | ||
title: Combining Providers | ||
--- | ||
For more information about `ObservableProvider`, [read this](/docs/concepts/providers) | ||
|
||
## Combining Providers | ||
In many situations, we may want to read the state of one provider within another provider | ||
Let's use the following example: | ||
```typescript | ||
import { of } from 'rxjs' | ||
const userIdProvider = new ObservableProvider(() => of('10')) | ||
``` | ||
|
||
We can now create another provider which is able to read our `userIdProvider` | ||
|
||
```typescript | ||
``` |
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,46 @@ | ||
--- | ||
title: Providers | ||
--- | ||
|
||
Now that we have installed ProviderX, let's talk about "providers". | ||
|
||
Providers allow to you to define and store state, and listen to the state from within your react application | ||
|
||
## Declaring a Provider | ||
You can define a provider as a global variable like so: | ||
|
||
```typescript | ||
import { of } from 'rxjs' | ||
const provider = new ObservableProvider(() => { | ||
return of('value') | ||
}) | ||
``` | ||
|
||
There are 4 important things to note here: | ||
- We are defining an `ObservableProvider`. This is the base provider class. There are multiple others providers. | ||
- All providers take in a function which return an `Observable` | ||
- All providers must return an RxJS `Observable` which is being done with `of` from `rxjs` | ||
- Observables can be of any type - `string`, `number`, `Promise`, etc. | ||
|
||
## What about for Asynchronous Operations? | ||
Many times you will need to perform asynchronous I/O in your application | ||
|
||
Here is how you would define a provider which contains an `Observable<Promise>` | ||
```typescript | ||
import { from } from 'rxjs' | ||
|
||
const provider = new ObservableProvider(() => { | ||
const fetchUser = async () => { | ||
const response = await fetch( | ||
'https://jsonplaceholder.typicode.com/users/1' | ||
) | ||
const json = await response.json() | ||
return json | ||
} | ||
const observable = from(fetchUser()) | ||
return observable | ||
}) | ||
``` | ||
- Here we are creating an asynchronous function to fetch an api result | ||
- We are then using the `from` function in RxJS to convert that `Promise` to an `Observable` | ||
- We then return the observable |
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,56 @@ | ||
--- | ||
title: Reading Providers | ||
--- | ||
import Tabs from "@theme/Tabs"; | ||
import TabItem from "@theme/TabItem"; | ||
|
||
For more information about `ObservableProvider`, [read this](/docs/concepts/providers) | ||
|
||
Because an RxJS `Observable` can be of any type, you only need to know one simple way to read | ||
providers from your frontend code. | ||
|
||
Using this `ObservableProvider`: | ||
```typescript | ||
import { from } from 'rxjs' | ||
|
||
const provider = new ObservableProvider(() => { | ||
const fetchUser = async () => { | ||
const response = await fetch( | ||
'https://jsonplaceholder.typicode.com/users/1' | ||
) | ||
const json = await response.json() | ||
return json | ||
} | ||
return from(fetchUser()) | ||
}) | ||
``` | ||
|
||
<Tabs | ||
groupId="providerx" | ||
defaultValue="react-providerx" | ||
values={[ | ||
{ label: 'React', value: 'react-providerx', }, | ||
]} | ||
> | ||
<TabItem value="react-providerx"> | ||
|
||
```tsx | ||
import { useProvider } from 'react-providerx' | ||
function Component() { | ||
const {isLoading, data, error} = useProvider(provider) | ||
|
||
if(isLoading) return <div> Loading... </div> | ||
if(error) return <div> Something went wrong... </div> | ||
|
||
return ( | ||
<div> | ||
{data} | ||
<button onClick={() => refresh(provider)}> | ||
Click to refresh | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
</TabItem> | ||
</Tabs> |
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
File renamed without changes.
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,4 @@ | ||
{ | ||
"extends": "@tsconfig/docusaurus/tsconfig.json", | ||
"include": ["src/"] | ||
} |
Oops, something went wrong.