React
Embed Semaphor dashboard in your React app
This section outlines step-by-step instructions for integrating the Semaphor dashboard into your React app. If you don’t have a React project yet, you can set one up using Vite's guide.
1. Install Semaphor package
Run the following command in your project directory to install the semaphor package:
2. Obtain the Auth Token
Before rendering a Dashboard, you need to retrieve an Auth Token. This token ensures secure access to your dashboard.
Use the following function to fetch the token:
Security Note
Do not expose DASHBOARD_SECRET
in client-side code in production. Use a
backend service to fetch the token securely.
3. Render the Dashboard
IMPORTANT: Make sure you import style.css
either at the beginning of your component or in your global CSS file. This step is necessary for correctly loading the dashboard styles.
Full Code
Here is the complete React code that uses the above steps. You can copy and paste this example into your application.
Key Considerations
- Security Note: Keep the
DASHBOARD_SECRET
secure and do not expose it in client-side code in production. This example is for demonstration purposes only. When deploying in production, obtain the authentication token from a secure, server-side environment. - Fetching the AuthToken: You need pass
AuthToken
to theDashboard
component. In this example, theuseEffect
hook is used to fetch the token when the component mounts.
Utility Hooks
Semaphor provides hooks to programmatically interact with the dashboard, allowing you to bring your own custom elements like date pickers, dropdowns, etc.
This is useful if you're looking to consolidate all filters into a dedicated filter panel within your app.
Currently, Semaphor provides the following hooks. More coming soon.
useTextFilter
useDateFilter
To use these hooks, you need to wrap the parent component with the SemaphorDataProvider
. This is required to ensure that the hooks are properly initialized.
Custom Filters
First, get the filterId
from the Semaphor console (found under the filter settings ⚙️ popover).
Now you can use the hooks inside the DashboardContainer
component to read and update the state of the filters.
Dropdown Example for Text Filters
Custom Placeholders
Semaphor allows you to customize default placeholders in the dashboard. For example, you can display a custom message or a custom image when the message when the no records are found or when the dashboard session is expired. This allows you to provide a more personalized experience that aligns with your application's branding.
You can customize the placeholders by passing the placeholders
prop in the Dashboard
compoent.
Note
You can pass any React.ReactNode
type to placeholders, which allows strings,
images, icons to be displayed.