0.1.95 • Published 7 months ago
@games-fun/react v0.1.95
@games-fun/react
React bindings and components for the Games.fun SDK.
Installation
npm install @games-fun/react @games-fun/sdk
Usage
Provider Setup
Wrap your application or game component with the GamesFunProvider
:
import { GamesFunProvider } from '@games-fun/react';
function App() {
return (
<GamesFunProvider
options={{
debug: true,
gameServerUrl: 'your-game-server-url'
}}
onInitialized={(connection) => console.log('SDK initialized', connection)}
onError={(error) => console.error('SDK error', error)}
>
<YourGame />
</GamesFunProvider>
);
}
Using the SDK in Components
import { useGamesFun, useGamesFunActions, ConnectionStatus } from '@games-fun/react';
function YourGame() {
const { connection } = useGamesFun();
// Register all game actions
const actions = useGamesFunActions({
purchaseItem: "Purchase in-game item",
useItem: "Use inventory item",
transferTokens: "Transfer tokens to game wallet"
});
const handlePurchase = async () => {
try {
await actions.purchaseItem({
itemId: 'sword',
price: 100,
});
console.log('Purchase successful');
} catch (error) {
console.error('Purchase failed:', error);
}
};
return (
<div>
<h1>My Game</h1>
{/* Built-in connection status component */}
<ConnectionStatus />
<button onClick={handlePurchase} disabled={!connection}>
Buy Sword (100 coins)
</button>
{connection && (
<div>
<p>Token Balance: {connection.tokenBalance}</p>
{connection.walletAddress && (
<p>Wallet: {connection.walletAddress}</p>
)}
</div>
)}
</div>
);
}
API Reference
Components
GamesFunProvider
- Context provider for the SDKConnectionStatus
- Component to display connection status
Hooks
useGamesFun
- Access the SDK instance and connectionuseGamesFunActions
- Register and trigger game actionsuseGamesFunContext
- Direct access to the context (advanced usage)
License
MIT