Skip to content

Instantly share code, notes, and snippets.

View Landerson352's full-sized avatar

Lincoln Anderson Landerson352

View GitHub Profile
@Landerson352
Landerson352 / genericKeyedComponent.tsx
Created August 10, 2022 22:18
A React component model that accepts various shapes of data by specifying the object properties to expect in the collection.
import React from 'react';
type BaseObject<ColorKey extends string, PercentKey extends string> = {
[key in ColorKey]: string;
} &
{
[key in PercentKey]: number;
};
function GenericListView<
@Landerson352
Landerson352 / machine.js
Last active March 18, 2021 14:30
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@Landerson352
Landerson352 / TilePuzzle.jsx
Created October 31, 2020 20:09
Sliding tile puzzle in React
import React from "react";
import { Box, Flex } from "@chakra-ui/core";
import _ from "lodash";
import { useKey } from "react-use";
import { motion } from "framer-motion";
const MotionFlex = motion.custom(Flex);
const TILE_SIZE = 100;
const ROWS = 4;
@Landerson352
Landerson352 / useOnUnmount.js
Last active August 21, 2020 16:38
A "componentWillUnmount" hook with dependencies.
import React from 'react';
// Usage: useOnUnmount((deps) => console.log(deps), deps);
const useOnUnmount = (fn, deps) => {
const ref = React.useRef(text);
React.useEffect(() => {
ref.current = deps;
}, [deps]);
@Landerson352
Landerson352 / NavigationBrowserScreen.js
Created December 10, 2019 15:40
React Navigation browser screen listing all routes
import React from 'react';
import { map } from 'lodash';
import { View } from 'react-native';
import { utils } from '../theme';
import RootNavigator from '../RootNavigator';
import ScreenView from '../components/ScreenView';
import Text from '../components/Text';
const RouteDisplay = ({ router, routeName, route, urlParts }) => {
@Landerson352
Landerson352 / InlineTextAndElements.js
Created December 10, 2019 15:38
React Native component for inline text and elements
import React from 'react';
import { View } from 'react-native';
import { flatMap, map } from 'lodash';
import { utils } from '../theme';
import Text from './Text';
const InlineTextAndElements = (props) => {
const { children, containerProps, ...restProps } = props;
import { useState } from 'react';
const useToggle = (initialState = false) => {
const [isActive, setIsActive] = useState(initialState);
const isInactive = !isActive;
const deactivate = () => setIsActive(false);
const activate = () => setIsActive(true);
const toggle = () => setIsActive(((value) => !value));
@Landerson352
Landerson352 / AppNavigator.js
Created September 16, 2019 00:08
React-Navigation starter files.
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import ROUTE from './route';
import HomeScreen from './screens/HomeScreen';
const createRouteConfig = ({ key, title }, screen) => ({
[key]: {
screen,
navigationOptions: {
@Landerson352
Landerson352 / useNavigation.js
Last active September 16, 2019 04:10
A hook for accessing the react-navigation prop.
import { useContext } from 'react';
import { NavigationContext } from 'react-navigation';
import { get } from 'lodash';
import ROUTE from '../route';
const useNavigation = () => {
const navigation = useContext(NavigationContext);
const routeName = get(navigation, 'state.routeName');
@Landerson352
Landerson352 / .editorconfig
Created September 15, 2019 23:00
A starter .editorconfig file.
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true