Local Development
This guide explains how to configure IC Reactor for local development against a local Internet Computer (IC) replica.
Prerequisites
Section titled “Prerequisites”- DFX SDK installed
- A local dfx project running (
dfx start)
Configuration
Section titled “Configuration”The easiest way to switch between local and mainnet environments is using ClientManager with the withProcessEnv option.
1. Setup ClientManager
Section titled “1. Setup ClientManager”import { ClientManager } from "@ic-reactor/react"
export const clientManager = new ClientManager({ // Auto-detects network based on DFX_NETWORK env var withProcessEnv: true,
// Optional: Explicitly define port if different from default (4943) // port: 8000})2. Configure Environment Variables
Section titled “2. Configure Environment Variables”For Vite, create or update your .env files:
Use this for local development:
DFX_NETWORK=local# Canister IDs from dfx canister id <name>VITE_BACKEND_CANISTER_ID=bkyz2-fmaaa-aaaaa-qaaaq-caiUse this for production build:
DFX_NETWORK=icVITE_BACKEND_CANISTER_ID=<production-canister-id>Fetching Root Key
Section titled “Fetching Root Key”When communicating with the IC mainnet, the agent uses a hardcoded public key to verify responses. For a local replica, the agent needs to fetch a different root key during initialization.
import { useEffect } from "react"import { clientManager, useAgentState } from "./reactor"
function App() { const { isInitialized } = useAgentState()
useEffect(() => { clientManager.initialize() }, [])
if (!isInitialized) { return <div>Loading...</div> }
return <YourApp />}Troubleshooting
Section titled “Troubleshooting””Fail to verify certificate”
Section titled “”Fail to verify certificate””This error usually means the agent hasn’t fetched the local root key.
Solution: Ensure you are calling clientManager.initialize() at the root of your application.
”Can’t connect to localhost:4943”
Section titled “”Can’t connect to localhost:4943””Check if your local replica is running and on which port.
-
Run
dfx info webserver-portto verify the port. -
If it’s not
4943, update yourClientManagerconfig:export const clientManager = new ClientManager({port: 8000, // or your specific portwithProcessEnv: true,})
Internet Identity on Localhost
Section titled “Internet Identity on Localhost”To use Internet Identity locally, you need to deploy a local instance of the II canister.
-
Pull the dependency in
dfx.json:{"canisters": {//..."internet_identity": {"type": "custom","candid": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity.did","wasm": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_dev.wasm.gz","specified_id": "rdmx6-jaaaa-aaaaa-aaadq-cai"}}} -
Deploy it:
It is recommended to deploy it to the default canister ID:
Terminal window dfx deploy internet_identity --specified-id rdmx6-jaaaa-aaaaa-aaadq-cai -
Login using the local provider URL.
useAuthhandles this automatically ifwithProcessEnvis set correctly!// Auto-detects based on network// Local: http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943// Mainnet: https://identity.ic0.appconst { login } = useAuth()