跳到主要内容
版本:v2

Capacitor 的 React Hooks

在其 Capacitor 应用中使用 React 的开发者可以访问一组有用的、社区维护的 React Hooks,以便在其 React 函数组件中访问 Capacitor API。

要安装 hooks:

npm install @capacitor-community/react-hooks

要使用 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 ]);

更多阅读

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