Skip to content

Instantly share code, notes, and snippets.

@AllanGraves
AllanGraves / App.js
Created December 24, 2020 12:05
Dynamic React Native Image Loading From Array
const DATA = [
{
text: "man",
image: require('./assets/images/photo1.jpg')
},
{
text: "woman",
image: require('./assets/images/photo2.jpg')
}
]
@AllanGraves
AllanGraves / App.js
Created December 28, 2020 11:08
Sample code to read file system in React
import * as FileSystem from 'expo-file-system';
<Button
onPress={() => {
let dirToRead = FileSystem.documentDirectory;
console.log('Reading :' + dirToRead);
FileSystem.readDirectoryAsync('file://' + dirToRead)
.then((dir) => {
dir.forEach((val) => {
@AllanGraves
AllanGraves / App.js
Created December 28, 2020 11:27
Download a File in React Native
import * as FileSystem from 'expo-file-system';
import {Image} from 'react-native';
const foo = require('./assets/images/photo1.jpg');
const fooURI = Image.resolveAssetSource(foo).uri;
<Button
onPress={() => {
let docDir = FileSystem.documentDirectory;
let localFile = 'file://' + docDir + 'photo1.jpg';
@AllanGraves
AllanGraves / App.js
Created December 28, 2020 12:15
Dynamically Load and Display an Image in React Native
import * as FileSystem from 'expo-file-system';
const foo = require('./assets/images/photo1.jpg');
const fooURI = Image.resolveAssetSource(foo).uri;
console.log('FooURI: ' + fooURI);
const App: () => React$Node = () => {
const [imageDownloaded, setImageDownloaded] = useState(0);
const docDir = FileSystem.documentDirectory;
const localFile = 'file://' + docDir + 'photo1.jpg';
@AllanGraves
AllanGraves / App.js
Created January 3, 2021 12:19
Initial Setup for a React Native Navigation Container
import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {SafeAreaView, ScrollView, View, Text, StatusBar} from 'react-native';
const App: () => React$Node = () => {
return (
<>
@AllanGraves
AllanGraves / App.js
Created January 3, 2021 12:39
3 simple screens
const ScreenOne = () => {
return (
<View>
<Text> Screen One </Text>
</View>
);
};
const ScreenTwo = () => {
return (
@AllanGraves
AllanGraves / App.js
Last active January 5, 2021 19:24
Setting up a basic Stack Navigator
import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {SafeAreaView, ScrollView, View, Text, StatusBar} from 'react-native';
const ScreenOne = () => {
return (
<View>
<Text> Screen One </Text>
@AllanGraves
AllanGraves / App.js
Created January 5, 2021 19:29
Changing our screens to have the ability to navigate
const ScreenOne = ( {navigation} ) => {
return (
<View>
<Text> Screen One </Text>
<Button
title="Go to Two"
onPress={() => navigation.navigate('Two')}
/>
</View>
);
@AllanGraves
AllanGraves / App.js
Created January 5, 2021 21:28
A small React State Hook to simulate the didComponentMount and willComponentUnmount React Life cycle functions
useEffect(() => {
console.log('Screen 1 Mount: ');
console.log(navigation.dangerouslyGetState());
return () => {
console.log('Screen 1 Unmount:');
console.log(navigation.dangerouslyGetState());
};
});
@AllanGraves
AllanGraves / App.js
Created January 6, 2021 20:35
Simple drawer navigator
const Drawer = createDrawerNavigator();
const DrawerScreens = () => {
return (
<Drawer.Navigator>
<Drawer.Screen name="One" component={ScreenOne} />
<Drawer.Screen name="Two" component={ScreenTwo} />
<Drawer.Screen name="Three" component={ScreenThree} />
</Drawer.Navigator>
);
};