Skip to content

Commit 08aae1f

Browse files
xmaxcookingTobbeJosh-Walker-GMdthyresson
authored
Update Toast Notifications Docs with Example of Async Toast using toast.promise (#9710)
Releated to #9641 @dthyresson --------- Co-authored-by: Tobbe Lundberg <[email protected]> Co-authored-by: Josh GM Walker <[email protected]> Co-authored-by: David Thyresson <[email protected]>
1 parent 375453a commit 08aae1f

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

docs/docs/toast-notifications.md

+77-10
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,98 @@ export default MainLayout
3636

3737
### Call the `toast` function
3838

39-
To render a toast notification, call the `toast` function or one of its methods:
39+
To render a basic toast notification with default styles, call the `toast` function:
4040

41-
```jsx title="web/src/components/PostForm/PostForm.js"
42-
// highlight-next-line
41+
```jsx title="web/src/layouts/MainLayout/MainLayout.js"
4342
import { toast } from '@redwoodjs/web/toast'
4443

4544
// ...
4645

4746
const PostForm = () => {
48-
const onSubmit = () => {
47+
const [create, { loading, error }] = useMutation(CREATE_POST_MUTATION)
48+
49+
const onSubmit = async (data) => {
4950
try {
50-
// Code to save a record...
51+
await create({ variables: { input: data }})
52+
// highlight-next-line
53+
toast('Post created')
54+
}
55+
catch (e) {
56+
// highlight-next-line
57+
toast('Error creating post')
58+
}
59+
}
60+
61+
return (
62+
// <Form onSubmit={onSubmit}> ... </Form>
63+
)
64+
})
65+
66+
export default PostForm
67+
```
68+
69+
### Call the `toast` variants
70+
71+
To render a toast notification with default icons and default styles, call the `toast` variants:
72+
73+
```jsx title="web/src/components/PostForm/PostForm.js"
74+
import { toast } from '@redwoodjs/web/toast'
75+
76+
// ...
77+
78+
const PostForm = () => {
79+
const [create, { loading, error }] = useMutation(CREATE_POST_MUTATION, {
80+
onCompleted: () => {
5181
// highlight-next-line
52-
toast('User created!')
53-
} catch (e) {
54-
// There's also methods for default styling:
82+
toast.success('Post created')
83+
}
84+
onError: () => {
5585
// highlight-next-line
56-
toast.error("Error creating post...")
86+
toast.error('Error creating post')
5787
}
88+
})
89+
90+
const onSubmit = (data) => {
91+
create({ variables: { input: data }})
5892
}
5993

6094
return (
61-
// JSX...
95+
// <Form onSubmit={onSubmit}> ... </Form>
6296
)
6397
})
6498

6599
export default PostForm
66100
```
101+
102+
or render an async toast by calling the `toast.promise` function:
103+
104+
```jsx title="web/src/components/PostForm/PostForm.js"
105+
import { toast } from '@redwoodjs/web/toast'
106+
107+
// ...
108+
109+
const PostForm = () => {
110+
const [create, { loading, error }] = useMutation(CREATE_POST_MUTATION)
111+
112+
const onSubmit = (data) => {
113+
// highlight-next-line
114+
toast.promise(create({ variables: { input: data }}), {
115+
loading: 'Creating post...',
116+
success: 'Post created',
117+
error: 'Error creating post',
118+
})
119+
}
120+
121+
return (
122+
// <Form onSubmit={onSubmit}> ... </Form>
123+
)
124+
})
125+
126+
export default PostForm
127+
```
128+
129+
:::warning
130+
131+
You can't use the [onError](https://www.apollographql.com/docs/react/api/react/hooks/#onerror) callback in combination with the `toast.promise` function.
132+
133+
:::

0 commit comments

Comments
 (0)