React bindings that make the async APIs from @meshsdk/hydra reactive with RxJS observables.
npm install @meshsdk/hydra-reactWrap your application (or the subtree that needs access) in HydraReactProvider. Then call useHydra() from any component to access the reactive Hydra client.
import { HydraReactProvider, useHydra } from "@meshsdk/hydra-react";
const HydraStatus = () => {
const { hydra, status, lastMessage } = useHydra();
const handleInit = () => {
hydra.connect().subscribe({
complete() {
hydra.init().subscribe();
},
});
};
return (
<section>
<p>Current status: {status ?? "unknown"}</p>
<pre>{JSON.stringify(lastMessage, null, 2)}</pre>
<button onClick={handleInit}>Init Hydra Head</button>
</section>
);
};
export const App = () => (
<HydraReactProvider
httpUrl="http://localhost:4001"
wsUrl="ws://localhost:4002"
autoConnect
>
<HydraStatus />
</HydraReactProvider>
);HydraReactProvider— initializes a single Hydra client instance and places it in React context. Pass the same constructor options you would giveHydraProvider(httpUrl,wsUrl, etc.).autoConnectprop — optional flag to callhydra.connect()automatically when the provider mounts.useHydra()— returns:hydra: proxy aroundHydraProvider. Every async method now yields anObservableyou can subscribe to or compose with RxJS operators.statusandlastMessage: React state snapshots of the latest status/message.status$andmessages$: the underlying observables for advanced scenarios.
Because each async method now emits through RxJS, you can combine Hydra calls with other streams:
import { mergeMap } from "rxjs";
const { hydra } = useHydra();
hydra.connect()
.pipe(mergeMap(() => hydra.newTx({
type: "Tx ConwayEra",
description: "Example",
cborHex: "...",
})))
.subscribe({
next(txId) {
console.log("Submitted tx", txId);
},
error(error) {
console.error("Hydra newTx failed", error);
},
});When the provider unmounts it tears down subscriptions automatically. If you need to dispose manually (for testing, etc.), call client.teardown() via the context or unmount the provider.
Please report issues or suggestions on the main Mesh SDK repository.