Scripts
You can fetch data from the blockchain using useFlowScript
:
import { useFlowScript } from '@doodlesteam/flooks';
const script = `
pub fun main(x: Int, y: Int): Int {
return x + y
}`;
function Component {
const { data } = useFlowScript<number>({
code: script,
args: [1, 2],
});
// Rest of the code...
}
Arguments are automatically parsed and validated from your script, so you don't have to specify each argument type manually.
You can optionally define the expected result type. In this case data
will be a number.
Flooks uses react-query (opens in a new tab) in the background, so you also get all its features, like retries and refetch intervals.
Doing the same with fcl:
import { useEffect, useState } from "react";
import * as fcl from "@onflow/fcl";
const script = `
pub fun main(x: Int, y: Int): Int {
return x + y
}`;
function Component {
const [result, setResult] = useState<number>();
useEffect(() => {
fcl.query({
cadence: script,
args: (arg, t) => [arg("1", t.Int), arg("2", t.Int)],
}).then((data) => {
setResult(data);
});
}, []);
// Rest of the code...
}