Skip to content

Commit

Permalink
updated readme, added changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
kosich committed Aug 7, 2020
1 parent b6d01a7 commit b04ab9d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

## [0.0.3] - 2020-08-07
### Added
- createElement$ constructor for dynamic elements
- a bunch of prefab elements

## [0.0.2]
### Added
- error handling

## [0.0.1]
### Added
- $ dynamic fragment
60 changes: 39 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<br/>
&lt;$&gt;
<br/>
<sub><sub>react fragment for RxJS content</sub></sub>
<sub><sub>react elements for RxJS content</sub></sub>
<br/>
<br/>
<a href="https://www.npmjs.com/package/react-rxjs-elements"><img src="https://img.shields.io/npm/v/react-rxjs-elements" alt="NPM"></a>
Expand All @@ -25,7 +25,13 @@ Simply add an Observable as one of `<$>`'s children:
<$>{ stream$ }</$>
```

`<$>` will subscribe to the `stream$` and will display it's updates in place.
or use a dynamic element, like $img

```tsx
<$img src={ stream$ } />
```

`react-rxjs-elements` will subscribe to the `stream$` and will display it's updates in place.
And it will clean up all subscriptions for you on component unmount.

Try it [**online**](https://stackblitz.com/edit/react-rxjs-elements?file=index.tsx)
Expand All @@ -38,55 +44,68 @@ npm i react-rxjs-elements

## 📖 Examples

A simple timer
A simple timer[online sandbox](https://stackblitz.com/edit/react-rxjs-elements-timer?file=index.tsx)

```tsx
import React from 'react';
import { timer } from 'rxjs';
import { $ } from 'react-rxjs-elements';

function App(){
function App() {
return <$>{ timer(0, 1000) } sec</$>
}
```

[online sandbox](https://stackblitz.com/edit/react-rxjs-elements-timer?file=index.tsx)
---

Dynamic image sources — [online sandbox](https://stackblitz.com/edit/react-rxjs-elements-img?file=index.tsx)

```tsx
import React from 'react';
import { timer } from 'rxjs';
import { map } from 'rxjs/operators';
import { $img } from 'react-rxjs-elements';

function App() {
const src$ = timer(0, 3000).pipe(
map(x => (x % 2) ? 'a.jpg' : 'b.jpg')
);

return <$img src={ src$ } />
}
```

---

A data fetch (with RxJS [fromFetch](https://rxjs.dev/api/fetch/fromFetch))
A data fetch (with RxJS [ajax](https://rxjs.dev/api/ajax/ajax)) — [online sandbox](https://stackblitz.com/edit/react-rxjs-elements-fetch?file=index.tsx)

```tsx
import React, { useMemo } from "react";
import { map, switchMap } from "rxjs/operators";
import { fromFetch } from "rxjs/fetch";
import { ajax } from "rxjs/ajax";
import { $ } from "react-rxjs-elements";


function App() {
const data$ = useMemo(() =>
fromFetch(URL).pipe(
switchMap(response => response.json()),
map(data => data.description)
)
ajax.getJSON(URL)
, []);

return <$>{ data$ }</$>
return <$>{data$}</$>;
}
```

[online sandbox](https://stackblitz.com/edit/react-rxjs-elements-fetch?file=index.tsx)

---

A counter
A counter[online sandbox](https://stackblitz.com/edit/react-rxjs-elements-counter?file=index.tsx)

```tsx
import React from 'react';
import { $ } from 'react-rxjs-elements';
import { $div } from 'react-rxjs-elements';
import { Subject } from 'rxjs';
import { startWith, scan } from 'rxjs/operators';

function App (){
function App() {
const subject$ = useMemo(() => new Subject(), []);

const output$ = useMemo(() =>
Expand All @@ -96,20 +115,19 @@ function App (){
)
, []);

return <div>
return <$div>
<button onClick={()=>subject$.next(-1)}>
-
</button>

<$>{ output$ }</$>
{ output$ /* results would be displayed in place */ }

<button onClick={()=>subject$.next(+1)}>
+
</button>
</div>
</$div>
}
```

[online sandbox](https://stackblitz.com/edit/react-rxjs-elements-counter?file=index.tsx)

## 🙂 Enjoy

0 comments on commit b04ab9d

Please sign in to comment.