Skip to content

Commit

Permalink
docs: write vue quick start guide (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjee authored Jan 24, 2024
1 parent 67d0452 commit 827ae4b
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion docs/framework/vue/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,70 @@ title: Quick Start
id: quick-start
---

# TODO
The basic vue app example to get started with the Tanstack vue-store.

**App.vue**
```html
<script setup>
import Increment from './Increment.vue';
import Display from './Display.vue';
</script>

<template>
<h1>How many of your friends like cats or dogs?</h1>
<p>Press one of the buttons to add a counter of how many of your friends like cats or dogs</p>
<Increment animal="dogs" />
<Display animal="dogs" />
<Increment animal="cats" />
<Display animal="cats" />
</template>
```

**store.js**
```js
import { Store } from '@tanstack/store';

// You can use @tanstack/store outside of Vue components too!
export const store = new Store({
dogs: 0,
cats: 0,
});

export function updateState(animal) {
store.setState((state) => {
return {
...state,
[animal]: state[animal] + 1,
};
});
}
```

**Display.vue**
```html
<script setup>
import { useStore } from '@tanstack/vue-store';
import { store } from './store';
const props = defineProps({ animal: String });
const count = useStore(store, (state) => state[props.animal]);
</script>

<!-- This will only re-render when `state[props.animal]` changes. If an unrelated store property changes, it won't re-render -->
<template>
<div>{{ animal }}: {{ count }}</div>
</template>
```

**Increment.vue**
```html
<script setup>
import { store, updateState } from './store';
const props = defineProps({ animal: String });
</script>

<template>
<button @click="updateState(animal)">My Friend Likes {{ animal }}</button>
</template>
```

0 comments on commit 827ae4b

Please sign in to comment.