React Google Drive picker hook
Google Drive API hook for React compatible with the newest Google auth API
Installation

General
- Add the
react-google-picker-hook
package
yarn add react-google-picker-hook
yarn add --dev @types/google.picker
npm install react-google-picker-hook --save
npm install @types/google.picker --save-dev
- Use the package
import { useGooglePicker } from 'react-google-picker-hook';
const [openPicker] = useGooglePicker(
(data: GoogleDrivePickerData, token: string) => {
console.log({ data, token });
if (data?.action === GOOGLE_ACTION.PICKED) {
}
},
{
googleAppId: 'googleAppId',
googleAuthClientId: 'googleAuthClientId',
googleAppKey: 'googleAppKey',
},
);
return <button onClick={openPicker}>Upload!</button>;
Handling errors (optional)
There are two separate error callback which can be used for error detection.
const [openPicker] = useGooglePicker(
{
onAuthFailed: (response: TokenResponse) => {
console.log(response.error_description || response.details);
},
tokenClientConfig: {
error_callback: ({ message, stack, type }: ErrorCallback) => {
console.log({ message, stack, type });
},
},
},
);
Extending the default config (optional)
Two custom configs objects
tokenClientConfig
is spread to Google's initTokenClient
customPickerConfig
is used to override the minimal default config used for picker
const [openPicker] = useGooglePicker(
{
customPickerConfig: {
},
tokenClientConfig: {
},
},
);
Two config callback functions
appendCustomPickerConfig
appendCustomViewConfig
Both used to extend the builder pattern of DocsView
and PickerBuilder
const [openPicker] = useGooglePicker(
{
appendCustomViewConfig: (view) => {
return view;
},
appendCustomPickerConfig: (picker) => picker.setSize(300, 300),
},
);