import { useQuery, useClient, useAuthState, Provider } from '@based/react'import based from '@based/client'// Create clientconst 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.