Skip to content

Commit b465d6c

Browse files
Merge pull request cerebral#9 from mfeitoza/master
Add svelte tutorial
2 parents f6d1a49 + 7341b47 commit b465d6c

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [React](views/react.md)
2525
* [Angular](views/angular.md)
2626
* [Vue](views/vue.md)
27+
* [Svelte](views/svelte.md)
2728

2829
## Addons
2930

views/svelte.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Svelte
2+
3+
There are two differente ways to connect Overmind to Svelte. You can use the **reactive declarations** or you can use the **reaction**.
4+
5+
When you connect Overmind to a component you ensure that whenever any tracked state changes, only components interested in that state will re-render.
6+
7+
## Reactive declarations
8+
9+
{% tabs %}
10+
{% tab title="overmind.js" %}
11+
```javascript
12+
import { createOvermind } from 'overmind'
13+
import { createMixin } from 'overmind-svelte'
14+
15+
const overmind = {
16+
state: {
17+
count: 0
18+
},
19+
actions: {
20+
increase({ state }) {
21+
state.count++;
22+
},
23+
decrease({ state }) {
24+
state.count--;
25+
}
26+
}
27+
}
28+
29+
const store = createMixin(createOvermind(overmind))
30+
31+
export const state = store.state
32+
export const actions = store.actions
33+
34+
```
35+
{% endtab %}
36+
37+
{% tabs %}
38+
{% tab title="App.svelte" %}
39+
```javascript
40+
41+
<script>
42+
import { state, actions } from './overmind.js'
43+
44+
$: count = $state.count
45+
</script>
46+
47+
<p>Count: {count}</p>
48+
<button id="increase" on:click={() => store.actions.increase()}>Increase</button>
49+
<button id="decrease" on:click={() => store.actions.decrease()}>Increase</button>
50+
```
51+
{% endtab %}
52+
53+
## Reactions
54+
55+
{% tabs %}
56+
{% tab title="overmind.js" %}
57+
```javascript
58+
import { createOvermind } from 'overmind'
59+
import { createMixin } from 'overmind-svelte'
60+
61+
const overmind = {
62+
state: {
63+
count: 0
64+
},
65+
actions: {
66+
increase({ state }) {
67+
state.count++;
68+
},
69+
decrease({ state }) {
70+
state.count--;
71+
}
72+
}
73+
}
74+
75+
const store = createMixin(createOvermind(overmind))
76+
77+
export const state = store.state
78+
export const actions = store.actions
79+
export const reactions = store.reactions
80+
81+
```
82+
{% endtab %}
83+
84+
{% tabs %}
85+
{% tab title="App.svelte" %}
86+
```javascript
87+
88+
<script>
89+
import { state, actions, reactions } from './overmind.js'
90+
91+
$: count = $state.count
92+
let doubled = undefined
93+
store.reaction(
94+
(state) => state.count,
95+
(value) => {
96+
doubled = value * 2
97+
},
98+
{
99+
immediate: true
100+
}
101+
)
102+
</script>
103+
104+
<p>Count: {count}</p>
105+
<p>Doubled: {doubled}</p>
106+
<button id="increase" on:click={() => store.actions.increase()}>Increase</button>
107+
<button id="decrease" on:click={() => store.actions.decrease()}>Increase</button>
108+
```
109+
{% endtab %}

0 commit comments

Comments
 (0)