React Integration

Games.fun provides a React SDK with hooks and components for easy integration. This guide shows how to use the React SDK in your game.

Installation

npm install @games-fun/react @games-fun/sdk

Provider Setup

Wrap your app with the Games.fun provider:

import { GamesFunProvider } from '@games-fun/react';

function App() {
    return (
        <GamesFunProvider 
            options={{ 
                debug: true,
                gameServerUrl: "your-validation-endpoint"
            }}
            onInitialized={(connection) => {
                console.log('SDK initialized:', connection);
            }}
            onError={(error) => {
                console.error('SDK error:', error);
            }}
        >
            <Game />
        </GamesFunProvider>
    );
}

Game Context

Create a game context to manage state:

Components

Authentication Component

Shop Component

Inventory Component

Hooks

useGamesFun

Access SDK connection and actions:

useGamesFunActions

Register and use game actions:

Error Handling

Handle common scenarios:

Development Mode

Enable dev mode for testing:

Best Practices

  1. State Management

    • Use context for game state

    • Handle loading states

    • Show error messages

    • Keep UI responsive

  2. Authentication

    • Check auth before actions

    • Handle token expiry

    • Show auth status

    • Clear invalid tokens

  3. Error Handling

    • Use error boundaries

    • Show helpful messages

    • Provide retry options

    • Log errors properly

  4. Development

    • Use dev mode locally

    • Test error cases

    • Mock responses

    • Check console logs

Next Steps

Last updated