Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readme examples #15

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Usually you will have a Store for each Page or Shared Resource that you need to
```javascript

const initialState = {
...yourInitialData
total: 0,
changed: false,
}

const { updateState, getCurrentState, state$ } = createStore(initialState);
Expand All @@ -63,7 +64,10 @@ export { yourState$ }

```javascript
const changeTotal = (quantity: number) => {
updateState({ total: 10 })
updateState({
total: quantity,
changed: true,
});
}
```

Expand All @@ -78,16 +82,17 @@ When you call `updateState`, your new data will be merged with the current data
The other side of the coin is the UsePlug Hook. This way you can Plug your component into your newly created Store and at each change, the store data update your component's state.

```javascript
const TestComponent = ({ name, total }) => {
const state = useStream(state$)
const TestComponent = ({ name }) => {
const { total, changed } = useStream(yourState$);
return (
<div>
<h1 data-testid="name">{name}</h1>
<p data-testid="total">Total: {total}</p>
<p data-testid="changed">The store was{changed ? '' : `n't`} changed.</p>
<button onClick={() => changeTotal(total + 1)}>Add more 1 to Total</button>
</div>
)
}
);
};
```

<hr />
Expand All @@ -102,13 +107,13 @@ If you use a older version of React without Hooks, you can use the Plug HOC to c
**1. Create your Container**

```javascript
const TestComponent = (props) => {
const { total, changed, name } = props;
const TestComponent = ({ name, total, changed }) => {
return (
<div>
<h1 data-testid="name">{name}</h1>
<p data-testid="total">Total: {total}</p>
<p data-testid="changed">The store was{changed ? '' : `n't`} changed.</p>
<button onClick={() => changeTotal(total + 1)}>Add more 1 to Total</button>
</div>
)
}
Expand All @@ -121,16 +126,16 @@ This stream receives your parent's component Prop, and this way you can merge it
```javascript
const streamToProp = (props) => {
return of(props).pipe(
combineLatest(state$),
map(([props, state]) => {
combineLatest(yourState$),
map(([{ name }, state]) => {
const data = {
name: props.name,
name,
...state
}
return data;
})
);
}
};
```

**3. Plug!**
Expand Down