0.1.3 • Published 1 year ago
@foretag.dev/react-surreal v0.1.3
React Surreal
A SurrealDB client for React, based on SurrealDB.js
Currently supports React 18 and above.
Usage
You can use this library with any React application / framework, here is an example with NextJS 13 App Dir.
// app/layout.tsx
'use client';
import React from 'react';
import Surreal from 'surrealdb.js';
import { SurrealProvider } from '@foretag.dev/react-surreal';
const db = new Surreal(process.env.NEXT_PUBLIC_SURREAL_HOST);
export default function RootLayout({ children, }: {
children: React.ReactNode
}) {
return <SurrealProvider
database={db}
>
<html>
<head></head>
<body>
{children}
</body>
</html>
</SurrealProvider>
}
The following hooks are provided:
useAuth
- Handles authentication to SurrealDB, this exports:signIn
,signOut
,signUp
methods &isAuth
import { useAuth } from '@foretag.dev/react-surreal';
const LoginPage = () => {
// ...
const { signIn, signOut, signUp, isAuth } = useAuth();
}
useQuery
- Queries to SurrealDB, this exports:data
,loading
,error
,refetch
,status
&time
import { useQuery } from '@foretag.dev/react-surreal';
const AccountPage = () => {
// ...
const { data, loading } = useQuery('SELECT * FROM account');
}