docs: add flutter_hooks code for optimistic updates
This commit is contained in:
@@ -227,8 +227,11 @@ separate mutations that has same data type but are triggered by different events
|
||||
|
||||
```dart
|
||||
MutationBuilder<Map<String, dynamic>, dynamic, Map<String, dynamic>, dynamic>(
|
||||
'sign-up?provider=$authProvider',
|
||||
(variable) => auth.signUp(variable, provider: authProvider),
|
||||
'sign-up?provider=$authProvider',
|
||||
(variable) => auth.signUp(variable, provider: authProvider),
|
||||
builder: (context, mutation) {
|
||||
/* ... */
|
||||
},
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
title: Optimistic Updates
|
||||
sidebar_position: 10
|
||||
---
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
> <em>In an optimistic update the UI behaves as though a change was successfully completed before receiving confirmation from the server that it actually was - it is being optimistic that it will eventually get the confirmation rather than an error. This allows for a more responsive user experience.</em>
|
||||
|
||||
@@ -11,10 +13,32 @@ sidebar_position: 10
|
||||
|
||||
When dealing with mutations that **update** documents/tables on the server, it's common for the new data to be automatically returned in the response of the `mutation`. Instead of refreshing any queries for that item and wasting a network call for data we already have, we can take advantage of the object returned by the mutation function and update the existing query with the new data immediately using the `Query.setData` method.
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="vanilla" label="Vanilla">
|
||||
|
||||
```dart
|
||||
final queryClient = QueryBowl.of(context);
|
||||
|
||||
return MutationBuilder<Todo, dynamic, Todo, dynamic>(
|
||||
'add-todo',
|
||||
(todo)=> api.addTodo(todo)
|
||||
onSuccess: (data, recoveryData) {
|
||||
// suppose a query with key 'todos' exists
|
||||
final query = queryClient.getQuery<Todo, dynamic>('todos');
|
||||
if(query == null) return;
|
||||
query.setData([...query.data, data]);
|
||||
},
|
||||
builder: /*...*/
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="flutter_hooks" label="Flutter Hooks">
|
||||
|
||||
```dart
|
||||
final queryClient = useQueryClient();
|
||||
|
||||
final mutation = useMutation<Todo, dynamic, Todo, dynamic>(
|
||||
'add-todo',
|
||||
(todo)=> api.addTodo(todo)
|
||||
onSuccess: (data, recoveryData) {
|
||||
@@ -26,10 +50,17 @@ return MutationBuilder<Todo, dynamic, Todo, dynamic>(
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
|
||||
## onMutate Callback
|
||||
|
||||
`onMutate` callback of `MutationBuilder` runs before the mutation is executed. It gives access to mutation variables in the Callback thus queries or any other source of data can be updated with predicted data to make the UI more instantaneous.
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="vanilla" label="Vanilla">
|
||||
|
||||
```dart
|
||||
final queryClient = QueryBowl.of(context);
|
||||
|
||||
@@ -52,5 +83,37 @@ return MutationBuilder<Todo, dynamic, Todo, List<Todo>>(
|
||||
if(query == null) return;
|
||||
query.setData(recoveryData);
|
||||
},
|
||||
builder: /*...*/
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="flutter_hooks" label="Flutter Hooks">
|
||||
|
||||
```dart
|
||||
final queryClient = useQueryClient();
|
||||
|
||||
final mutation = useMutation<Todo, dynamic, Todo, List<Todo>>(
|
||||
'add-todo',
|
||||
(todo)=> api.addTodo(todo)
|
||||
onMutate: (variable) {
|
||||
final query = queryClient.getQuery<Todo, dynamic>('todos');
|
||||
if(query == null) return;
|
||||
query.setData([...query.data, variable]);
|
||||
|
||||
// here we should be able to return a previous snapshot
|
||||
// of the intended query data which can be used when
|
||||
// an error occurs in mutation & we can rollback to a previous
|
||||
// data set
|
||||
return query.data;
|
||||
},
|
||||
onError: (data, recoveryData) {
|
||||
final query = queryClient.getQuery<Todo, dynamic>('todos');
|
||||
if(query == null) return;
|
||||
query.setData(recoveryData);
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
@@ -3,13 +3,13 @@ sidebar_position: 1
|
||||
id: overview
|
||||
---
|
||||
|
||||
# Overview
|
||||
## Overview
|
||||
|
||||
Asynchronous data caching, refetching & invalidation library for Flutter. FL-Query lets you manage & distribute your async data without touching any global state
|
||||
|
||||
Fl-Query makes asynchronous server state management a breeze in flutter
|
||||
|
||||
# Features
|
||||
## Features
|
||||
|
||||
- Async data caching & management
|
||||
- Smart + effective refetching
|
||||
@@ -20,7 +20,7 @@ Fl-Query makes asynchronous server state management a breeze in flutter
|
||||
- Easy to write & understand code. Follows DRY (Don't repeat yourself) convention
|
||||
- Compatible with both vanilla Flutter & elite [flutter_hooks](https://pub.dev/packages/flutter_hooks)
|
||||
|
||||
# Installation
|
||||
## Installation
|
||||
|
||||
Regular installation:
|
||||
|
||||
@@ -34,11 +34,11 @@ For elite flutter_hooks user (Welcome to the flutter cool community btw😎)
|
||||
$ flutter pub add flutter_hooks fl_query_hooks
|
||||
```
|
||||
|
||||
# Docs
|
||||
## Docs
|
||||
|
||||
You can find the documentation of fl-query at https://fl-query.vercel.app/
|
||||
You can find the documentation of fl-query at https://fl-query.krtirtho.dev/
|
||||
|
||||
# Basic Usage
|
||||
## Basic Usage
|
||||
|
||||
Initialize the cache databases in your `main` method
|
||||
|
||||
@@ -161,7 +161,7 @@ class MyApp extends HookWidget{
|
||||
|
||||
*To master the fl-query follow the official blog at https://fl-query.krtirtho.dev/blog*
|
||||
|
||||
# Why?
|
||||
## Why?
|
||||
<p align="center">
|
||||
<img src="https://media.giphy.com/media/1M9fmo1WAFVK0/giphy.gif" alt="The hell, why?"/>
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user