Frappe React SDK

1.14.0 · active · verified Sun Apr 19

The `frappe-react-sdk` is a JavaScript library providing a collection of React hooks designed to simplify interaction with a Frappe Framework backend. It offers abstractions for common tasks such as user authentication (cookie-based or token-based), comprehensive database operations (fetching single documents, lists, counts, creating, updating, and deleting records), file uploads with progress tracking, and generic API calls to whitelisted backend functions. The current stable version is `1.14.0`. The library demonstrates an active release cadence, with updates typically every few weeks addressing bug fixes, dependency updates, and feature enhancements. Under the hood, it leverages `frappe-js-sdk` for Frappe API communication and `SWR` for efficient data fetching, caching, and automatic revalidation, providing a fast and reactive UI experience by default.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic setup using `FrappeProvider` and a simple authentication flow with `useFrappeAuth`.

import { FrappeProvider, useFrappeAuth } from 'frappe-react-sdk';
import React from 'react';

function AuthStatus() {
  const { currentUser, login, logout, get and maintain user state } = useFrappeAuth();

  const handleLogin = async () => {
    await login('username', 'password'); // Replace with actual credentials or user input
    console.log('Logged in as:', currentUser.fullname);
  };

  const handleLogout = async () => {
    await logout();
    console.log('Logged out');
  };

  return (
    <div>
      {currentUser ? (
        <p>Welcome, {currentUser.fullname || 'User'}! <button onClick={handleLogout}>Logout</button></p>
      ) : (
        <p>Please log in. <button onClick={handleLogin}>Login</button></p>
      )}
    </div>
  );
}

function App() {
  // The URL is optional if the web app is hosted on the same URL as the Frappe server.
  // Use process.env.FRAPPE_SERVER_URL or a similar environment variable for production.
  return (
    <FrappeProvider url={process.env.FRAPPE_SERVER_URL ?? 'https://my-frappe-server.frappe.cloud'}>
      <h1>Frappe React SDK Demo</h1>
      <AuthStatus />
      {/* Your other app components would go here */}
    </FrappeProvider>
  );
}

view raw JSON →