Skip to content

useEtherInput Hook

The useEtherInput hook converts between ETH and USD for a given input and display mode. It returns both values, the current native currency price, and loading/error flags so you can build responsive inputs.

Import

import { useEtherInput } from "@scaffold-ui/hooks";

Usage

const {
  valueInEth,
  valueInUsd,
  nativeCurrencyPrice,
  isNativeCurrencyPriceLoading,
  isNativeCurrencyPriceError,
} = useEtherInput({ value, usdMode });

Parameters

ParameterTypeDefaultDescription
valuestring-The current input value. Interpreted as ETH when usdMode is false, USD when true.
usdModeboolean-If true, value is USD; if false, value is ETH.

Return Values

PropertyTypeDescription
valueInEthstringThe input value expressed in ETH. Empty string while conversion is unavailable.
valueInUsdstringThe input value expressed in USD. Empty string while conversion is unavailable.
nativeCurrencyPricenumberThe fetched native currency price in USD. 0 when not yet available.
isNativeCurrencyPriceLoadingbooleanLoading state for fetching the native currency price.
isNativeCurrencyPriceErrorbooleanError state for fetching the native currency price.

Live Examples

Basic Usage

Fetching price...

import React, { useState } from "react";
import { useEtherInput } from "@scaffold-ui/hooks";
 
function EtherUsdConverter() {
  const [value, setValue] = useState("");
  const [usdMode, setUsdMode] = useState(false);
  const { valueInEth, valueInUsd, isNativeCurrencyPriceLoading } = useEtherInput({ value, usdMode });
 
  return (
    <div>
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <input
          value={value}
          onChange={(e) => setValue(e.target.value)}
          placeholder={usdMode ? "Enter amount in USD" : "Enter amount in ETH"}
        />
        <button onClick={() => setUsdMode((m) => !m)}>{usdMode ? "USD" : "ETH"}</button>
      </div>
      {isNativeCurrencyPriceLoading ? (
        <p>Fetching price...</p>
      ) : (
        <p>
          ETH: <strong>{valueInEth || "–"}</strong> | USD: <strong>{valueInUsd || "–"}</strong>
        </p>
      )}
    </div>
  );
}

With Error Handling

ETH: | USD: 1000

import React, { useState } from "react";
import { useEtherInput } from "@scaffold-ui/hooks";
 
function ConverterWithErrors() {
  const [value, setValue] = useState("1000");
  const [usdMode] = useState(true);
  const { valueInEth, valueInUsd, isNativeCurrencyPriceError } = useEtherInput({ value, usdMode });
 
  if (isNativeCurrencyPriceError) {
    return <p style={{ color: "red" }}>Price feed unavailable. Showing entered value: ${valueInUsd}</p>;
  }
 
  return <p>ETH: {valueInEth || "–"} | USD: {valueInUsd || "–"}</p>;
}