ice/packages/plugin-request
github-actions[bot] e3ca513f56
chore: update versions (#5949)
* chore: update versions

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: ClarkXia <xiawenwu41@gmail.com>
2023-03-02 13:55:59 +08:00
..
src fix: request plugin type and usage (#5992) 2023-03-01 18:00:59 +08:00
CHANGELOG.md chore: update versions (#5949) 2023-03-02 13:55:59 +08:00
README.md
hooks.d.ts fix: request plugin type and usage (#5992) 2023-03-01 18:00:59 +08:00
package.json chore: update versions (#5949) 2023-03-02 13:55:59 +08:00
request.d.ts fix: request plugin type and usage (#5992) 2023-03-01 18:00:59 +08:00
runtime.d.ts fix: import path of types and runtime (#5981) 2023-02-28 15:08:10 +08:00
tsconfig.json
types.d.ts fix: import path of types and runtime (#5981) 2023-02-28 15:08:10 +08:00

README.md

@ice/plugin-request

Provides a unified request method for ice.js projects.

Usage

$ npm i @ice/plugin-request -S

Add plugin.

import { defineConfig } from '@ice/app';
import request from '@ice/plugin-request';

export default defineConfig(() => ({
  plugins: [
    request(),
  ],
}));

API

request

import { request } from 'ice';

export async function getUser(id) {
  return await request(`/api/user/${id}`);
}

useRequest

import { useEffect } from 'react';
import { useRequest } from 'ice';

export default function Home() {
  const {
    data,
    error,
    loading,
    request
  } = useRequest(service.getUser);

  useEffect(() => {
    request();
  }, []);

  if (error) {
    return <div>failed to load</div>;
  }
  if (!data || loading) {
    return <div>loading...</div>;
  }
  return (
    <h2 className={styles.title}>
      Name: {data.name} Age: {data.age}
    </h2>
  );
}