Skip to content

Commit 43b62e0

Browse files
authored
tmm/use select pause (#15)
* feat: add pause to select * fix: types
1 parent dcb135a commit 43b62e0

25 files changed

+133
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ npm install --save react-supabase @supabase/supabase-js
2020

2121
Create a Supabase client and pass it to the `Provider`:
2222

23-
```js
23+
```tsx
2424
import { createClient } from '@supabase/supabase-js'
2525
import { Provider } from 'react-supabase'
2626

@@ -35,7 +35,7 @@ const App = () => (
3535

3636
Now every component inside and under the `Provider` can use the Supabase client and hooks:
3737

38-
```js
38+
```tsx
3939
import { useRealtime } from 'react-supabase'
4040

4141
const Todos = () => {

docs/pages/documentation/auth/use-auth-state-change.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Receive a notification every time an auth event happens. Composed in the [`useAuth` recipe](/recipes/use-auth).
44

5-
```js highlight=4,5,6
5+
```tsx highlight=4,5,6
66
import { useAuthStateChange } from 'react-supabase'
77

88
function Page() {

docs/pages/documentation/auth/use-reset-password.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Sends reset request to email address.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useResetPassword } from 'react-supabase'
77

88
function Page() {
@@ -23,7 +23,7 @@ function Page() {
2323

2424
During hook initialization:
2525

26-
```js
26+
```tsx
2727
const [{ error, fetching }, resetPassword] = useResetPassword({
2828
options: {
2929
redirectTo: 'https://example.com/welcome',
@@ -33,7 +33,7 @@ const [{ error, fetching }, resetPassword] = useResetPassword({
3333

3434
Or execute function:
3535

36-
```js
36+
```tsx
3737
const { error } = await resetPassword('user@example.com', {
3838
redirectTo: 'https://example.com/reset',
3939
})

docs/pages/documentation/auth/use-signin.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Log in existing user, or login via a third-party provider.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useSignIn } from 'react-supabase'
77

88
function Page() {
@@ -27,7 +27,7 @@ function Page() {
2727

2828
During hook initialization:
2929

30-
```js
30+
```tsx
3131
const [{ error, fetching, session, user }, signIn] = useSignIn({
3232
options: {
3333
redirectTo: 'https://example.com/dashboard',
@@ -37,7 +37,7 @@ const [{ error, fetching, session, user }, signIn] = useSignIn({
3737

3838
Or the execute function:
3939

40-
```js
40+
```tsx
4141
const { error, session, user } = await signIn(
4242
{
4343
email: 'user@example.com',
@@ -53,15 +53,15 @@ const { error, session, user } = await signIn(
5353

5454
Omit password from the execute function:
5555

56-
```js
56+
```tsx
5757
const { error, session, user } = await signIn({ email: 'user@example.com' })
5858
```
5959

6060
## Third-party providers
6161

6262
Either pass a provider (and scopes) during hook initialization:
6363

64-
```js
64+
```tsx
6565
const [{ error, fetching, user, session }, signIn] = useSignIn({
6666
provider: 'github',
6767
options: {
@@ -72,7 +72,7 @@ const [{ error, fetching, user, session }, signIn] = useSignIn({
7272

7373
Or execute function:
7474

75-
```js
75+
```tsx
7676
const { error, session, user } = await signIn(
7777
{ provider: 'github' },
7878
{ scopes: 'repo gist notifications' },

docs/pages/documentation/auth/use-signout.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Remove logged in user and trigger a `SIGNED_OUT` event.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useSignOut } from 'react-supabase'
77

88
function Page() {

docs/pages/documentation/auth/use-signup.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Creates new user.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useSignUp } from 'react-supabase'
77

88
function Page() {
@@ -27,7 +27,7 @@ function Page() {
2727

2828
During hook initialization:
2929

30-
```js
30+
```tsx
3131
const [{ error, fetching, session, user }, signUp] = useSignUp({
3232
options: {
3333
redirectTo: 'https://example.com/dashboard',
@@ -37,7 +37,7 @@ const [{ error, fetching, session, user }, signUp] = useSignUp({
3737

3838
Or execute function:
3939

40-
```js
40+
```tsx
4141
const { error, session, user } = await signUp(
4242
{
4343
email: 'user@example.com',

docs/pages/documentation/data/use-delete.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Performs DELETE on table.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useDelete } from 'react-supabase'
77

88
function Page() {
@@ -24,7 +24,7 @@ Throws error during execute if a filter is not passed during hook initialization
2424

2525
During hook initialization:
2626

27-
```js
27+
```tsx
2828
const [{ count, data, error, fetching }, execute] = useDelete('todos', {
2929
filter: (query) => query.eq('status', 'completed'),
3030
options: {
@@ -36,7 +36,7 @@ const [{ count, data, error, fetching }, execute] = useDelete('todos', {
3636

3737
Or execute function:
3838

39-
```js
39+
```tsx
4040
const { count, data, error } = await execute((query) => query.eq('id', id), {
4141
returning: 'minimal',
4242
count: 'estimated',

docs/pages/documentation/data/use-filter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Creates dynamic filter for using with other hooks.
44

5-
```js highlight=4,5,6,7,8,9,10,11
5+
```tsx highlight=4,5,6,7,8,9,10,11
66
import { useFilter, useSelect } from 'react-supabase'
77

88
function Page() {

docs/pages/documentation/data/use-insert.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Performs INSERT into table.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useInsert } from 'react-supabase'
77

88
function Page() {
@@ -22,7 +22,7 @@ function Page() {
2222

2323
During hook initialization:
2424

25-
```js
25+
```tsx
2626
const [{ count, data, error, fetching }, execute] = useInsert('todos', {
2727
options: {
2828
returning: 'represenation',
@@ -33,7 +33,7 @@ const [{ count, data, error, fetching }, execute] = useInsert('todos', {
3333

3434
Or execute function:
3535

36-
```js
36+
```tsx
3737
const { count, data, error } = await execute(
3838
{ name: 'Buy more cheese' },
3939
{

docs/pages/documentation/data/use-select.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Performs vertical filtering with SELECT.
44

5-
```js highlight=4
5+
```tsx highlight=4
66
import { useSelect } from 'react-supabase'
77

88
function Page() {
@@ -20,14 +20,15 @@ function Page() {
2020

2121
During hook initialization:
2222

23-
```js
23+
```tsx
2424
const [{ count, data, error, fetching }, reexecute] = useSelect('todos', {
2525
columns: 'id, name, description',
2626
filter: (query) => query.eq('status', 'completed'),
2727
options: {
2828
count: 'exact',
2929
head: false,
3030
},
31+
pause: false,
3132
})
3233
```
3334

@@ -37,7 +38,7 @@ When using dynamic filters, you must make sure filters aren’t recreated every
3738

3839
The easiest way to avoid this is to create dynamic filters with the [`useFilter`](/documentation/data/use-filter) hook:
3940

40-
```js
41+
```tsx
4142
import { useState } from 'react'
4243
import { useFilter, useSelect } from 'react-supabase'
4344

@@ -52,3 +53,33 @@ function Page() {
5253
return ...
5354
}
5455
```
56+
57+
## Pausing `useSelect`
58+
59+
In some cases, we may want `useSelect` to execute when a pre-condition has been met, and not execute otherwise. For instance, we may be building a form and want validation to only take place when a field has been filled out.
60+
61+
We can do this by setting the `pause` option to `true`:
62+
63+
```tsx
64+
import { useState } from 'react'
65+
import { useFilter, useSelect } from 'react-supabase'
66+
67+
function Page() {
68+
const [username, setUsername] = useState(null)
69+
const filter = useFilter((query) => query.eq('username', username), [
70+
username,
71+
])
72+
const [result, reexecute] = useSelect('users', {
73+
filter,
74+
pause: !username,
75+
})
76+
77+
return (
78+
<form>
79+
<label>Enter a username</label>
80+
<input onChange={(e) => setUsername(e.target.value)} />
81+
{result.data && <div>Username is taken</div>}
82+
</form>
83+
)
84+
}
85+
```

0 commit comments

Comments
 (0)