5.2.1 • Published 9 months ago

@based/react v5.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

@based/react

Wraps the @based/client into react hooks

import { useQuery, useClient, useAuthState,  Provider } from '@based/react'
import based from '@based/client'

// Create client
const client = based({
  env: 'myEnv',
  org: 'myOrg',
  project: 'myProject',
})

export const Authenticate = ({ children }) => {
  // Observes id a user is authenticated
  const authState = useAuthState()

  // Returns the based client from the provider
  const client = useClient()

  if (authState.token) {
    // When authenticated render the app
    return children
  }

  return <button onClick={() => {
     // Authenticate and use localStorage
     await client.setAuthState({
      token: 'my-token',
      persistent: true,
     })
   }} />
}

export const Something = () => {
  // Subscribes to data
  const { data, error, loading } = useQuery('db', {
    children: { $list: true, id: true, name: true },
  })

  return <div>{loading ? 'loading...' : data.children.map(
    { id, name } => <p onClick={() => {
        client.call('db:delete', { id })
    }} key={id}>{name}</p>)
  }</div>
}

export const App = () => {
  return <Provider client={client}>
    <Authenticate><Something /></Authenticate>
  </Provider>
}

useQuery

Subscribes when a component gets mounted / unsubscribes when a comment gets unmounted. Query hooks are automaticly cached and share remote active subscriptions.

import { useQuery } from '@based/react'

export const Something = () => {
  const { data, error, loading } = useQuery('someQueryFunction')
  if (error) {
    return error.message
  }
  return <div>{loading ? 'loading...' : data.text}</div>
}

The persistent option will store the cached result of a query in localStorage.

const { data: userInfo } = useQuery(
  'someUserInfo',
  {
    id: client.authState.userId,
  },
  { persistent: true }
)

useQuery allows passing a null value to the function name - this is usefull when you have a query depending on other data

const { data: userInfo } = useQuery('someUserInfo', {
  id: client.authState.userId,
})

const { data } = useQuery(
  userInfo.preferedLanguage ? 'someQueryFunction' : null,
  {
    preferedLanguage: userInfo.preferedLanguage,
  }
)

useClient

Returns the based client from the Provider

import { useClient, Provider } from '@based/react'
import based from '@based/client'

// Create client
const client = based({
  env: 'myEnv',
  org: 'myOrg',
  project: 'myProject',
})

export const Something = () => {
  const client = useClient()
  useEffect(() => {
    client.call('domSomething')
  }, [])
}

export const App = () => {
  return (
    <Provider client={client}>
      <Something />
    </Provider>
  )
}

useConnected

Observes the connected state of the based client.

import { useConnected } from '@based/react'

export const Something = () => {
  const isConnected = useConnected()
  if (isConnected) {
    return 'connected!'
  }
  return 'not connected :('
}

useLoading

Observes if any active useQuery hook is loading.

import { useLoading } from '@based/react'

export const Something = () => {
  const isLoading = useLoading()
  if (isLoading) {
    return 'some data is loading'
  }
  return 'everything is loaded'
}

useAuthState

Observe if a client is authenticated.

import { useAuthState } from '@based/react'

export const Something = () => {
  const authState = useAuthState()
  if (authState.token) {
    return `User ${authState.userId} is authenticated`
  }
  if (authState.error) {
    console.log('An error authenticating', authState.error)
  }
  return 'not authenticated'
}
5.1.0

9 months ago

5.2.1

9 months ago

5.2.0

9 months ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

2 years ago

4.5.4

2 years ago

4.5.3

2 years ago

4.4.0

2 years ago

4.6.1

2 years ago

4.6.0

2 years ago

4.5.0

2 years ago

4.5.2

2 years ago

4.5.1

2 years ago

4.2.7

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.6

2 years ago

4.2.5

2 years ago

4.2.4

2 years ago

4.2.3

2 years ago

4.2.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.0

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

4.0.0

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.5.0

3 years ago

2.3.2

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.5.2

3 years ago

2.3.4

3 years ago

2.5.1

3 years ago

2.3.3

3 years ago

2.3.5

3 years ago

0.7.2

3 years ago

0.7.3

3 years ago

0.7.1

3 years ago

0.5.0

3 years ago

0.4.0

4 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.3.0

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago