Skip to content

Commit dcebf73

Browse files
docs(website): improve guide by adding action trigger example
1 parent 01cedb5 commit dcebf73

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

packages/overmind-website/examples/api/app_initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const overmind = new Overmind({
3737
actions
3838
})
3939
40-
export const connect = createConnect(app)
40+
export const connect = createConnect(overmind)
4141
`,
4242
},
4343
]
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
const javascript = {
2+
react: [
3+
{
4+
fileName: 'Posts.js',
5+
target: 'jsx',
6+
code: `
7+
import React from 'react'
8+
import { connect } from '../overmind'
9+
10+
class Posts extends React.Component {
11+
componentDidMount() {
12+
this.props.overmind.actions.loadPosts()
13+
}
14+
render() {
15+
const { overmind } = this.props
16+
17+
if (overmind.state.isLoadingPosts) {
18+
return <h4>Loading posts...</h4>
19+
}
20+
21+
return (
22+
<ul>
23+
{overmind.state.posts.map(post => <li key={post.id}>{post.title}</li>)}
24+
</ul>
25+
)
26+
}
27+
}
28+
29+
export default connect(Posts)
30+
`,
31+
},
32+
],
33+
vue: [
34+
{
35+
fileName: 'Posts.vue (template)',
36+
target: 'markup',
37+
code: `
38+
<h4 v-if="overmind.state.isLoadingPosts">
39+
Loading posts...
40+
</h4>
41+
<ul v-else>
42+
<li v-for="post in overmind.state.posts">
43+
{{post.title}}
44+
</li>
45+
</ul>
46+
`,
47+
},
48+
{
49+
fileName: 'Posts.vue (script)',
50+
code: `
51+
import { connect } from '../overmind'
52+
53+
export default connect({
54+
mounted() {
55+
this.overmind.actions.loadPosts()
56+
}
57+
})
58+
`,
59+
},
60+
],
61+
}
62+
63+
const typescript = {
64+
react: [
65+
{
66+
fileName: 'components/Posts.tsx',
67+
code: `
68+
import * as React from 'react'
69+
import { Connect, connect } from '../overmind'
70+
71+
class Posts extends React.Component<Connect> {
72+
componentDidMount() {
73+
this.props.overmind.actions.loadPosts()
74+
}
75+
render() {
76+
const { overmind } = this.props
77+
78+
if (overmind.state.isLoadingPosts) {
79+
return <h4>Loading posts...</h4>
80+
}
81+
82+
return (
83+
<ul>
84+
{overmind.state.posts.map(post => <li key={post.id}>{post.title}</li>)}
85+
</ul>
86+
)
87+
}
88+
}
89+
90+
export default connect(Posts)
91+
`,
92+
},
93+
],
94+
vue: javascript.vue,
95+
angular: [
96+
{
97+
fileName: 'posts.component.ts',
98+
code: `
99+
import { Component } from '@angular/core';
100+
import { connect } from '../overmind'
101+
102+
@Component({
103+
selector: 'posts-list',
104+
template: \`
105+
<h4 *ngIf="overmind.state.isLoadingPosts">
106+
Loading posts...
107+
</h4>
108+
<ul *ngIf="!overmind.state.isLoadingPosts">
109+
<li *ngFor="let post of overmind.state.posts">
110+
{{post.title}}
111+
</li>
112+
</ul>
113+
\`
114+
})
115+
@connect()
116+
export class PostsList {
117+
ngOnInit() {
118+
this.overmind.actions.loadPosts()
119+
}
120+
}
121+
`,
122+
},
123+
],
124+
}
125+
126+
export default (ts, view) => (ts ? typescript[view] : javascript[view])

packages/overmind-website/guides/beginner/01_getstarted.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ Let us see how we define this effect called **jsonPlaceholder**.
4848

4949
## Effects
5050

51+
We can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in the example below. Doing this injection keeps our actions pure and Overmind knows when these injected libraries are accessed.
52+
5153
```marksy
5254
h(Example, { name: "guide/getstarted/effects" })
5355
```
5456

55-
We can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in this example. Doing this injection keeps our actions pure and Overmind knows when these injected libraries are accessed.
57+
Finally we need to trigger our action and we will do that when the component mounts.
58+
59+
```marksy
60+
h(Example, { name: "guide/getstarted/triggeraction" })
61+
```
5662

5763
## Devtools
5864

0 commit comments

Comments
 (0)