@ice/plugin-request
v1.0.3
Published
Request plugin for ice.
Downloads
192
Keywords
Readme
@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>
);
}