跳到主要内容
版本:v2

Capacitor 的 React Hooks

在 Capacitor 应用中使用 React 的开发者可以利用这套由社区维护的实用 React Hooks,轻松地在 React 函数组件中调用 Capacitor API。

安装 hooks:

npm install @capacitor-community/react-hooks

使用时只需在函数组件中导入即可:

import { useFilesystem, base64FromPath, availableFeatures } from '@capacitor-community/react-hooks/filesystem';

const MyComponent = () => (
const { readFile } = useFilesystem();

useEffect(() => {
const readMyFile = async () => {
const file = await readFile({
path: filepath,
directory: FilesystemDirectory.Data
});
// ...
}

readMyFile();
}, [ readFile ]);

扩展阅读

所有可用 hooks 的文档请参阅 @capacitor-community/react-hooks 代码仓库。