-
Notifications
You must be signed in to change notification settings - Fork 19
docs(example): ensnode-react #1999
base: main
Are you sure you want to change the base?
Changes from 1 commit
feba95e
bf92c54
bb1b42c
8a23e61
4827992
f3d7abc
2d8f70a
681dcc1
6b8aaf2
1e386f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2026 NameHash | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # ensnode-react Example | ||
|
|
||
| A minimal React app demonstrating how to use `@ensnode/ensnode-react` to resolve | ||
| an address' Mainnet Primary Name (via `usePrimaryName`). | ||
|
|
||
| By default it connects to the NameHash-hosted alpha ENSNode at | ||
| `https://api.alpha.ensnode.io`. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| pnpm install | ||
| pnpm -F @ensnode/ensnode-react-example dev | ||
| ``` | ||
|
|
||
| To point at a different ENSNode, set `VITE_ENSNODE_URL`: | ||
|
|
||
| ```bash | ||
| VITE_ENSNODE_URL=http://localhost:4334 pnpm -F @ensnode/ensnode-react-example dev | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>ensnode-react Example</title> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/main.tsx"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "@ensnode/ensnode-react-example", | ||
| "private": true, | ||
| "version": "0.0.1", | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "vite build", | ||
| "preview": "vite preview", | ||
| "typecheck": "tsgo --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@ensnode/ensnode-react": "workspace:*", | ||
| "@ensnode/ensnode-sdk": "workspace:*", | ||
| "@tanstack/react-query": "^5.62.14", | ||
| "enssdk": "workspace:*", | ||
| "react": "catalog:", | ||
| "react-dom": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/react": "catalog:", | ||
| "@types/react-dom": "catalog:", | ||
| "@vitejs/plugin-react": "^4.5.2", | ||
| "typescript": "catalog:", | ||
| "vite": "catalog:" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { StrictMode } from "react"; | ||
|
|
||
| import { createEnsNodeProviderOptions, EnsNodeProvider } from "@ensnode/ensnode-react"; | ||
|
|
||
| import { PrimaryNameView } from "./PrimaryNameView"; | ||
|
|
||
| const ENSNODE_URL = import.meta.env.VITE_ENSNODE_URL ?? "https://api.alpha.ensnode.io"; | ||
|
|
||
| const options = createEnsNodeProviderOptions({ url: ENSNODE_URL }); | ||
|
|
||
| export function App() { | ||
| return ( | ||
| <StrictMode> | ||
| <EnsNodeProvider options={options}> | ||
| <h1> | ||
| <code>ensnode-react</code> Example App | ||
| </h1> | ||
| <p> | ||
| Connected to <code>{ENSNODE_URL}</code> | ||
| </p> | ||
| <PrimaryNameView /> | ||
| </EnsNodeProvider> | ||
| </StrictMode> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { Address, ChainId } from "enssdk"; | ||
| import { useState } from "react"; | ||
|
|
||
| import { usePrimaryName } from "@ensnode/ensnode-react"; | ||
|
|
||
| const DEFAULT_ADDRESS: Address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; // vitalik.eth | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All address values we use in this app should use |
||
| const MAINNET: ChainId = 1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| export function PrimaryNameView() { | ||
| const [address, setAddress] = useState<Address>(DEFAULT_ADDRESS); | ||
| const [input, setInput] = useState<string>(DEFAULT_ADDRESS); | ||
|
|
||
| const { data, isLoading, error } = usePrimaryName({ | ||
| address, | ||
| chainId: MAINNET, | ||
| accelerate: true, | ||
| }); | ||
|
|
||
| return ( | ||
| <div> | ||
| <h2>Primary Name</h2> | ||
| <p> | ||
| Resolves the ENSIP-19 Mainnet Primary Name for an address using <code>usePrimaryName</code>. | ||
| </p> | ||
|
|
||
| <form | ||
| onSubmit={(event) => { | ||
| event.preventDefault(); | ||
| setAddress(input as Address); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @notrab I need to stop seeing code like this. Ever. It completely goes against our culture as a team and company. I'm going completely crazy at how many times I have to repeat the related ideas. There's so many problems in this 1 line of code I'm not going to spend the time to explain them all over and over again. |
||
| }} | ||
|
|
||
| > | ||
| <input | ||
| type="text" | ||
| value={input} | ||
| onChange={(event) => setInput(event.target.value)} | ||
| placeholder="0x..." | ||
| style={{ width: "28rem" }} | ||
| /> | ||
|
|
||
| <button type="submit">Resolve</button> | ||
| </form> | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| {isLoading && <p>Loading...</p>} | ||
| {error && <p>Error: {error.message}</p>} | ||
| {data && ( | ||
| <p> | ||
| Primary Name: <strong>{data.name ?? "(none)"}</strong> | ||
| </p> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { createRoot } from "react-dom/client"; | ||
|
|
||
| import { App } from "./App"; | ||
|
|
||
| // biome-ignore lint/style/noNonNullAssertion: the #root element definitely exists (see index.html) | ||
| createRoot(document.getElementById("root")!).render(<App />); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /// <reference types="vite/client" /> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "jsx": "react-jsx", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "lib": ["ESNext", "DOM", "DOM.Iterable"] | ||
| }, | ||
| "include": ["src"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import react from "@vitejs/plugin-react"; | ||
| import { defineConfig } from "vite"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [react()], | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this example app to achieve its goals, we need it to implement more critical ideas:
I believe that Option 1 is the direction that 99% of our customers will want to use as there are benefits to knowing the ENS namespace even before a successful connection to ENSNode is established. Therefore this example app should implement Option 1.
That means this example app should also read an ENS namespace as an environment variable that defaults to mainnet.