0
0
React Nativemobile~20 mins

File system access in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File System Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Reading a file content in React Native
What will be the output shown on the screen after running this React Native code snippet that reads a file content asynchronously?
React Native
import React, { useEffect, useState } from 'react';
import { Text, View } from 'react-native';
import * as FileSystem from 'expo-file-system';

export default function App() {
  const [content, setContent] = useState('');

  useEffect(() => {
    async function readFile() {
      const fileUri = FileSystem.documentDirectory + 'test.txt';
      await FileSystem.writeAsStringAsync(fileUri, 'Hello React Native');
      const data = await FileSystem.readAsStringAsync(fileUri);
      setContent(data);
    }
    readFile();
  }, []);

  return (
    <View>
      <Text>{content}</Text>
    </View>
  );
}
AThe screen is blank with no text
BThe screen displays: [object Promise]
CThe screen displays: Hello React Native
DThe app crashes with a runtime error
Attempts:
2 left
💡 Hint
Remember that reading files asynchronously returns a Promise, so you must await it before setting state.
📝 Syntax
intermediate
1:30remaining
Correct syntax for writing a file in React Native
Which option shows the correct syntax to write the string 'Data saved' to a file named 'output.txt' using Expo FileSystem in React Native?
Aawait FileSystem.writeFileAsync('output.txt', 'Data saved');
BFileSystem.writeFile(FileSystem.documentDirectory + 'output.txt', 'Data saved');
CFileSystem.writeAsString(FileSystem.documentDirectory + 'output.txt', 'Data saved');
Dawait FileSystem.writeAsStringAsync(FileSystem.documentDirectory + 'output.txt', 'Data saved');
Attempts:
2 left
💡 Hint
Check the exact method name and parameters in Expo FileSystem documentation.
lifecycle
advanced
1:30remaining
File read timing in React Native component lifecycle
In which React Native lifecycle phase is it best to read a file asynchronously to ensure the UI updates with the file content after the component mounts?
AInside useEffect with an empty dependency array []
BInside useEffect with no dependency array
CDirectly inside the component body before return
DInside the constructor function
Attempts:
2 left
💡 Hint
Think about when side effects like file reading should happen in React functional components.
🔧 Debug
advanced
1:30remaining
Debugging file read error in React Native
You run this code but get an error: "File does not exist" when reading a file. What is the most likely cause?
React Native
const fileUri = FileSystem.documentDirectory + 'notes.txt';
const content = await FileSystem.readAsStringAsync(fileUri);
console.log(content);
AThe file 'notes.txt' was never created or written before reading
BYou must import 'fs' module to read files in React Native
CreadAsStringAsync requires a callback function as second argument
DThe fileUri path is incorrect because documentDirectory is undefined
Attempts:
2 left
💡 Hint
Check if the file exists before reading it.
🧠 Conceptual
expert
2:00remaining
Understanding file system permissions on mobile devices
Which statement best describes file system access permissions in React Native apps on iOS and Android?
AApps can freely read and write any file anywhere on the device without restrictions
BApps have sandboxed storage areas and need explicit permissions to access external storage on Android
CiOS allows apps to write files anywhere on the device but Android restricts to app folders only
DReact Native automatically grants all file system permissions without user consent
Attempts:
2 left
💡 Hint
Think about mobile OS security and app sandboxing.