### Install Document Picker Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/install.mdx Install the document picker package using yarn. ```bash yarn add @react-native-documents/picker ``` -------------------------------- ### Bare React Native Setup Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/install.mdx After installing the package for a bare React Native project, run pod install in the ios directory and then rebuild your project with Xcode. ```bash # Install the package first cd ios pod install # Then rebuild your project with Xcode ``` -------------------------------- ### Start Local Development Server Source: https://github.com/react-native-documents/document-picker/blob/main/docs/README.md Starts a local development server for live preview. Changes are reflected without server restart. ```bash yarn start ``` -------------------------------- ### Install Document Viewer Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/install.mdx Install the document viewer package using yarn. ```bash yarn add @react-native-documents/viewer ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/react-native-documents/document-picker/blob/main/docs/README.md Run this command to install project dependencies using Yarn. ```bash yarn ``` -------------------------------- ### Bare React Native iOS Installation Source: https://context7.com/react-native-documents/document-picker/llms.txt For bare React Native projects on iOS, navigate to the `ios` directory and run `pod install`. ```bash cd ios && pod install ``` -------------------------------- ### Configure Jest Setup Files Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/sponsor-only/jest-mocks.md Add these paths to your Jest configuration's `setupFiles` array to enable the provided mocks for document picker and viewer modules. These mocks simulate successful calls by default. ```json { "setupFiles": [ "./node_modules/@react-native-documents/picker/jest/build/jest/setup.js", "./node_modules/@react-native-documents/viewer/jest/build/jest/setup.js" ] } ``` -------------------------------- ### Expo Installation Source: https://context7.com/react-native-documents/document-picker/llms.txt For Expo projects, a development build is required. Run `expo prebuild --clean` followed by `expo run:ios` or `expo run:android`. ```bash expo prebuild --clean expo run:ios # or expo run:android ``` -------------------------------- ### V2 DocumentPicker Usage Source: https://github.com/react-native-documents/document-picker/wiki/V2-to-V3-migration Example of how to use DocumentPicker in V2 with a callback. ```javascript DocumentPicker.show({ filetype: [DocumentPickerUtil.images()], }, (error,res) => { console.log( res.uri, res.type, // mime type res.fileName, res.fileSize ); }); ``` -------------------------------- ### Expo Development Build Setup Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/install.mdx Steps to set up a development build for Expo apps when using custom native code. This is required for packages like @react-native-documents/picker and @react-native-documents/viewer as they include native modules. ```bash # install the package first # Build the app locally expo prebuild --clean expo run:ios expo run:android ``` -------------------------------- ### V3 DocumentPicker Pick with Async/Await Source: https://github.com/react-native-documents/document-picker/wiki/V2-to-V3-migration Example of using DocumentPicker.pick with async/await in V3. Includes error handling for cancellations. ```javascript try { const res = await DocumentPicker.pick({ type: [DocumentPicker.types.images], }); console.log( res.uri, res.type, // mime type res.name, res.size ); } catch ( err ) { if ( DocumentPicker.isCancel(err) ) { // User cancelled the picker, exit any dialogs or menus and move on } else { throw err; } } ``` -------------------------------- ### Pick a directory with pickDirectory Source: https://context7.com/react-native-documents/document-picker/llms.txt Opens a folder/directory picker. Use `requestLongTermAccess: true` to get persistent read/write access across app restarts. Returns a `DirectoryPickerResponse`. ```tsx import { pickDirectory, releaseLongTermAccess, releaseSecureAccess } from '@react-native-documents/picker' import { Platform } from 'react-native' // One-time directory access const handlePickDir = async () => { const { uri } = await pickDirectory({ requestLongTermAccess: false }) console.log('Selected directory URI:', uri) } // Persistent directory access (survives app restart) const handlePickDirLongTerm = async () => { const result = await pickDirectory({ requestLongTermAccess: true }) if (result.bookmarkStatus === 'success') { // Persist bookmark to storage await AsyncStorage.setItem('dirBookmark', result.bookmark) console.log('Directory URI:', result.uri) } // Release when done if (Platform.OS === 'android') { await releaseLongTermAccess([result.uri]) } else { await releaseSecureAccess([result.uri]) } } ``` -------------------------------- ### Open Single File (Open Mode) Source: https://context7.com/react-native-documents/document-picker/llms.txt Use `mode: 'open'` to get a reference to the original file instead of a copy. This reference is valid until the app is terminated. ```tsx // Open mode — single file, returns a reference to the original const handleOpenFile = async () => { const [file] = await pick({ mode: 'open', type: [types.pdf, types.docx] }) console.log(file.uri) // Direct reference to original; valid until app is terminated ``` -------------------------------- ### V3 DocumentPicker Pick with Promises Source: https://github.com/react-native-documents/document-picker/wiki/V2-to-V3-migration Example of using DocumentPicker.pick with Promises in V3. Includes error handling for cancellations. ```javascript DocumentPicker.pick({type: [DocumentPicker.types.images]}) .then((res) => { console.log( res.uri, res.type, // mime type res.name, res.size ); }) .catch((err) => { if ( DocumentPicker.isCancel(err) ) { // User cancelled the picker, exit any dialogs or menus and move on } else { throw err; } }); ``` -------------------------------- ### Replace pickSingle with pick Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/sponsor-only/migration.md Substitute the 'pickSingle' method with 'pick'. The 'pick' method returns an array of files, so destructure the first element to get the single result. ```typescript const result = await pickSingle(options) ``` ```typescript const [result] = await pick(options) ``` -------------------------------- ### Determine File Type Information Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/sponsor-only/picker/limiting-selectable-files.md Use `isKnownType` to get MIME type and file extension information from a given value (UTType, MIME type, or extension). This helps in constructing the `type` parameter for `pick()`. ```ts import { isKnownType } from '@react-native-documents/picker' const { isKnown, mimeType, preferredFilenameExtension } = isKnownType({ kind: 'extension', value: 'pdf', }) ``` -------------------------------- ### Picking a File in Open Mode Source: https://github.com/react-native-documents/document-picker/blob/main/docs/docs/sponsor-only/picker/open-mode.mdx Demonstrates how to initiate the document picker in 'open' mode to select a single file. Includes basic error handling. ```tsx import { pick } from '@react-native-documents/picker' return (