Getting Started
This guide will walk you through the installation and a basic implementation of @blocklet/did-space-react
. You'll learn how to add a connection button and display the connected DID Space information.
Installation#
First, add the package to your project using npm or your preferred package manager.
npm install @blocklet/did-space-react
Basic Usage#
Here is a minimal example of how to use DIDSpaceConnect
and DIDSpaceConnection
together. This setup displays a button to connect to a DID Space. After a successful connection, it will show a card with the space's details, and the button remains available to connect to a different space.
import React, { useState } from 'react';
import { DIDSpaceConnect, DIDSpaceConnection, type DIDSpaceGateway } from '@blocklet/did-space-react';
import Toast from '@arcblock/ux/lib/Toast';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
export default function App() {
const [spaceGateway, setSpaceGateway] = useState<DIDSpaceGateway | null>(null);
const handleSuccess = async ({ spaceGateway: newSpaceGateway }: { spaceGateway: DIDSpaceGateway }) => {
try {
// Store the spaceGateway object to display its information
setSpaceGateway(newSpaceGateway);
Toast.success('Successfully connected to DID Space.');
} catch (error) {
console.error(error);
const message = error instanceof Error ? error.message : 'An error occurred during connection.';
Toast.error(message);
}
};
return (
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
<Box>
<Typography variant="body1" gutterBottom>
Click the button below to connect to a DID Space. You can use it again to switch to a different space.
</Typography>
<DIDSpaceConnect onSuccess={handleSuccess} />
</Box>
{spaceGateway && (
<Box>
<Typography variant="h6" component="h3" gutterBottom>
Connected Space Information
</Typography>
<DIDSpaceConnection
endpoint={spaceGateway.endpoint}
selected
/>
</Box>
)}
</Box>
);
}
How It Works#
- State Management: We use the
useState
hook to store thespaceGateway
object, which holds the details of the connected DID Space. It's initialized tonull
. - Connection Button: The
DIDSpaceConnect
component is rendered to handle the connection flow. When a user clicks it and successfully connects, itsonSuccess
callback is triggered. - Updating State: The
onSuccess
handler receives thespaceGateway
object and updates the component's state usingsetSpaceGateway
. This triggers a re-render. - Displaying Information: The
DIDSpaceConnection
component is conditionally rendered. It only appears if thespaceGateway
state is notnull
. It receives theendpoint
from the state and uses it to fetch and display the space's details. - Switching Spaces: Because
DIDSpaceConnect
remains visible, the user can initiate a new connection at any time. A successful connection will update the state with the newspaceGateway
object, and theDIDSpaceConnection
card will automatically update to show the new space's information.
Next Steps#
Now that you have a basic setup, you can explore the components in more detail to customize their appearance and behavior.