Skip to content

Commit

Permalink
docs: updated basic example to include actions example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Mar 17, 2022
1 parent bf32454 commit d98a7cc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
45 changes: 45 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,51 @@ export const setFirstName = mutation('set-first-name', (state, payload) => state
export const setLastName = mutation('set-last-name', (state, payload) => state.lastName = payload);
```

Or if you're using actions:

```typescript
import actionExtension from '@harlem/extension-action';

import {
createStore
} from '@harlem/core';

const STATE = {
firstName: 'John',
lastName: 'Smith'
};

export const {
state,
getter,
mutation,
action,
...store
} = createStore('user', STATE, {
extensions: [
actionExtension()
]
});

export const fullName = getter('fullname', state => {
return `${state.firstName} ${state.lastName}`;
});

export const loadUserData = action('load-user-data', async (id: string, mutate, { signal }) => {
const response = await fetch(`/api/users/${id}`, { signal });

const {
firstName,
lastName,
} = await response.json();

mutate(state => {
state.firstName = firstName;
state.lastName = lastName;
});
});
```

4. Use your store in your app:
```html
<template>
Expand Down
51 changes: 48 additions & 3 deletions docs/src/guide/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,64 @@ export const {
...store
} = createStore('user', STATE);

export const fullName = getter('fullName', state => {
export const fullName = getter('fullname', state => {
return `${state.firstName} ${state.lastName}`;
});

export const setFirstName = mutation<string>('setFirstName', (state, payload) => {
export const setFirstName = mutation('set-first-name', (state, payload: string) => {
state.firstName = payload;
});

export const setLastName = mutation<string>('setLastName', (state, payload) => {
export const setLastName = mutation('set-last-name', (state, payload: string) => {
state.lastName = payload;
});
```

Or if you're using actions:

```typescript
import actionExtension from '@harlem/extension-action';

import {
createStore
} from '@harlem/core';

const STATE = {
firstName: 'John',
lastName: 'Smith'
};

export const {
state,
getter,
mutation,
action,
...store
} = createStore('user', STATE, {
extensions: [
actionExtension()
]
});

export const fullName = getter('fullname', state => {
return `${state.firstName} ${state.lastName}`;
});

export const loadUserData = action('load-user-data', async (id: string, mutate, { signal }) => {
const response = await fetch(`/api/users/${id}`, { signal });

const {
firstName,
lastName,
} = await response.json();

mutate(state => {
state.firstName = firstName;
state.lastName = lastName;
});
});
```

## Use your store in your app

To use your store in your app just import the parts you need and the rest is history.
Expand Down

1 comment on commit d98a7cc

@vercel
Copy link

@vercel vercel bot commented on d98a7cc Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.