How to use the react-use/lib/useAsync function in react-use

To help you get started, we’ve selected a few react-use examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ArcBlock / forge-js / apps / forge-web / src / pages / dashboard / components / metrics.js View on Github external
function Metrics({ theme, sparkline, itemSize, size }) {
  const state = useAsync(sparkline ? fetchBoth : fetchLatest);
  const [updates, setUpdates] = useState(null);
  const [liveUpdate, setLiveUpdate] = useLiveUpdate();
  const [animation, setAnimation] = useBoolean(false);
  const [token] = useTokenInfo();

  // Pull
  useInterval(
    async () => {
      try {
        let res = null;
        if (sparkline) {
          res = await fetchBoth();
        } else {
          res = await fetchLatest();
        }
        setUpdates(res);
github ArcBlock / forge-js / apps / forge-web / src / pages / explorer / components / paginated_list.js View on Github external
function PaginatedList({ args, pageSize, pageLink, dataKey, dataLoaderFn, dataRenderFn }) {
  const [cursor, setCursor] = useState(null);

  // eslint-disable-next-line
  const fetchList = async (args, size, cur = null) => {
    const params = { ...args, paging: { size: size || 10 } };
    if (cur) {
      params.paging.cursor = cur;
    }

    const res = await dataLoaderFn(params);

    return res;
  };

  const state = useAsync(async () => fetchList(args, pageSize, cursor), [args, pageSize, cursor]);

  if (state.error) {
    console.error(state.error);
  }

  if (state.loading) {
    return (
      
        
      
    );
  }

  if (state.error) {
    return (
github ArcBlock / forge-js / apps / forge-web / src / libs / hooks.js View on Github external
export function useStartupInfo() {
  const [, setTokenInfo] = useTokenInfo();
  const [, setNodeInfo] = useNodeInfo();

  const state = useAsync(fetchInfo);
  if (state.value) {
    // HACK: we must add a timeout here
    setTimeout(() => {
      setTokenInfo(state.value.token);
      setNodeInfo(state.value.node);
    }, 0);
  }

  useInterval(async () => {
    try {
      const result = await fetchInfo();
      localStorage.setItem(`${process.env.REACT_APP_NAME}.token`, JSON.stringify(result.token));
      localStorage.setItem(`${process.env.REACT_APP_NAME}.node`, JSON.stringify(result.node));
    } catch (err) {
      // Do nothing
    }
github ArcBlock / forge-js / apps / forge-web / src / pages / dashboard / components / status / index.js View on Github external
export default function StatusSection() {
  const state = useAsync(fetchStatus);
  const [selected, setSelected] = useState(null);

  if (state.loading) {
    return (
github ArcBlock / forge-js / apps / forge-web / src / pages / explorer / home / producer.js View on Github external
export default function ProducerGlobe() {
  const state = useAsync(fetchPeers);
  const { width } = useWindowSize();
  const [theme] = useThemeMode();
  const [liveUpdate] = useLiveUpdate();
  const [activeMarkerId, setActiveMarker] = useState(null);

  useInterval(
    async () => {
      if (state.value) {
        const proposer = await fetchLatestProposer();
        setActiveMarker(proposer);
      }
    },
    liveUpdate ? 5000 : null
  );

  if (state.loading) {
github ArcBlock / forge-js / apps / forge-web / src / pages / dashboard / components / top_accounts.js View on Github external
export default function TopAccounts({ sparkline }) {
  const state = useAsync(fetchTopAccounts);
  const [token] = useTokenInfo();

  if (state.loading) {
    return ;
  }

  if (state.error) {
    return <p>{state.error.message}</p>;
  }

  return (
    
        
          Rank
          Username<table>
      </table>
github ArcBlock / forge-js / apps / forge-web / src / pages / dashboard / components / account_activity.js View on Github external
export default function AccountActivity({ data, delayMS }) {
  const state = useAsync(async () =&gt; formatActivity(data, delayMS), [data, delayMS]);

  if (state.loading) {
    return ;
  }

  if (state.error) {
    return <p>{state.error.message}</p>;
  }

  return (
    
      
    
  );
}
github ArcBlock / forge-js / examples / nextjs-keystone-starter / client / pages / paid.js View on Github external
export default function PaymentPage() {
  const state = useAsync(fetchStatus);
  const [open, toggle] = useToggle(false);

  if (state.loading) {
    return (
      
        <main>
          
        </main>
      
    );
  }

  if (state.error) {
    return (
      
        <main>{state.error.message}</main>
github ArcBlock / forge-js / apps / forge-web / src / pages / dashboard / components / top_validators.js View on Github external
export default function TopValidators({ sparkline }) {
  const state = useAsync(fetchTopValidators);
  const [currentPage, setCurrentPage] = useState(1);

  if (state.loading) {
    return ;
  }

  if (state.error) {
    return <p>{state.error.message}</p>;
  }

  const renderPagination = () =&gt; {
    if (state.value.length &gt; pageSize) {
      return (
github ArcBlock / forge-js / examples / nextjs-keystone-starter / client / hooks / session.js View on Github external
export default function useSession() {
  const state = useAsync(fetchSession);
  return state;
}