Skip to content

Instantly share code, notes, and snippets.

RN Coding Snippets / stuff one might need one day

Android

Reading from the default shared preferences file

// requires access to the activity
val sharedPreferencesFile = activity?.get()?.let {
 it.getSharedPreferences("${it.packageName}_preferences", Context.MODE_PRIVATE)
const padArrayEnd = <T>(arr: T[], targetLength: number, val: T): T[] => {
if (arr.length >= targetLength) return [...arr]
return arr.concat(Array(targetLength - arr.length).fill(val))
}
console.log(padArrayEnd(["0", "1"], 3, ""))
console.log(padArrayEnd(["0", "1", "2"], 3, ""))
console.log(padArrayEnd(["0", "1", "2", "3"], 3, ""))
import React from 'react';
import { NavigationStackScreenProps } from 'react-navigation-stack';
interface Props extends NavigationStackScreenProps {
foo: string;
}
const TestScreen = ({ navigation, foo }: Props) => {
const barDirectly = navigation.getParam('bar');
return <Text>bar from navigation: {barDirectly} bar as foo from HOC: {foo}</Text>;
@ca057
ca057 / develop-react-native-m1.md
Last active April 25, 2021 10:36
Develop React Native on Macbook Pro M1

What helped me to get everything running on my Apple Macbook with the M1 chip:

@ca057
ca057 / withClearStateOnAction.ts
Created September 8, 2020 08:57
higher-order reducer to clear redux state on specific action
import { combineReducers, Action } from '@reduxjs/toolkit';
const withClearStateOnAction = <S = any>(clearStateAction: Action) => (
reducer: Reducer<S | undefined, Action>,
) => (state: S, action: Action) => {
if (clearStateAction.type === action.type) {
return reducer(undefined, clearStateAction);
}
return reducer(state, action);
};
@ca057
ca057 / rn-dev-commands.md
Last active June 16, 2022 14:33
Commands for RN development

iOS / Mac

List all devices & simulators available on your Mac:

xcrun simctl list devices available
@ca057
ca057 / exif-to-json.md
Last active May 7, 2022 16:37
Extracting EXIF data into JSON

command line tools for working with images to json

Extract all exif data as list into a json file:

exiftool -j -q dir > data.json

Sort the exif data by the original creation time:

jq 'sort_by(.DateTimeOriginal)' data.json
@ca057
ca057 / densities.md
Last active March 8, 2021 12:49
Table showing densities of assets for mobile apps and their qualifier/factors on iOS & Android
Density Android density qualifier iOS scaling factor
Low-density (~120dpi) ldpi not supported
Medium-density (~160dpi) mdpi original scale
High-density (~240dpi) hdpi not supported
Extra-high-density (~320dpi) xhdpi @2x
Extra-extra-high-density (~480dpi) xxhdpi @3x
Extra-extra-extra-high-density (~640dpi) xxxhdpi @4x

source: https://developer.android.com/studio/write/resource-manager#automatic-parsing

@ca057
ca057 / condResolveEmptyList.js
Created April 3, 2018 07:09
Apollo HOF to conditionally resolve with an empty list
/**
* Curried higher-order function taking a predicate and an actual resolver function. In case the
* predicate returns true, an empty list is returned, otherwise the result of the actual resolver function.
*
* Both the predicate and the resolver function will get the all arguments as a standard resolver function.
*/
const condResolveEmptyList = predicate => resolver => (
data,
args,
context,
@ca057
ca057 / expressRequestDurationMiddleware.js
Last active March 29, 2018 13:47
Middleware to measure request duration for express using the Performance Timing API.
const { performance } = require('perf_hooks');
const express = require('express');
const expressRequestDurationMiddleware = (req, res, next) => {
performance.mark('request.start');
const end = res.end;
res.end = (...args) => {
performance.mark('request.end');