Skip to content

Instantly share code, notes, and snippets.

@bvaughn
bvaughn / trimming–demo-videos.md
Last active Nov 10, 2020
Handy tip for trimming demo videos
View trimming–demo-videos.md

Handy tip for trimming demo videos

Thought I'd document the process I used for trimming down my React conf demo videos in case it might be useful to someone else in the future.

Let's say I have a 40 second video named original-video.mp4, and I want to only keep the following sections (in seconds): 1-8, 10-11, 20-33

I could create 3 chunks using ffmpeg like so:

ffmpeg -i original-video.mp4 -ss 1 -t 7 piece-a.mp4 -ss 10 -t 1 piece-b.mp4 -ss 20 -t 33 piece-c.mp4
View react-window-prerendering-virtual-rows.md

Pre-rendering virtual rows

Example list with ten total rows and a viewport large enough to display two rows.

Legend

█ - Visible row ▓ - Hidden/display-locked row ░ - Empty space (nothing rendered here)

View profiling-data.v2.json
{
"version": 2,
"profilingSummary": {
"commitDurations": [
2.555000042775646,
0.0450000079581514,
0.054999967687763274,
2.4800000683171675,
0.03999999898951501,
0.0350000336766243,
@bvaughn
bvaughn / profiling-a-chrome-extension.md
Last active May 7, 2020
Profiling a custom Chrome extension
View profiling-a-chrome-extension.md

Chrome's profiler ("Performance tab) is very useful for measuring JavaScript performance, but what if you want to measure the performance of a custom extension?

For example, what if I would like to profile the following interaction:


The interaction we want to profile


@bvaughn
bvaughn / profile-data.json
Created Apr 1, 2019
Sample React DevTools profiling data export
View profile-data.json
{
"version": 1,
"profilingSummary": {
"commitDurations": [
8.130000089295208,
2.7099999133497477,
0.40499994065612555,
0.3299998352304101,
0.3400000277906656,
0.33000006806105375,
@bvaughn
bvaughn / example.jsx
Last active May 16, 2021
Advanced example for manually managing subscriptions in an async-safe way using hooks
View example.jsx
import React, { useMemo } from "react";
import useSubscription from "./useSubscription";
// In this example, "source" is an event dispatcher (e.g. an HTMLInputElement)
// but it could be anything that emits an event and has a readable current value.
function Example({ source }) {
// In order to avoid removing and re-adding subscriptions each time this hook is called,
// the parameters passed to this hook should be memoized.
const subscription = useMemo(
() => ({
@bvaughn
bvaughn / infinite-lists-and-reflow.md
Last active May 24, 2021
Infinite lists and reflow
View infinite-lists-and-reflow.md

Infinite lists and reflow

In my experience, infinite lists use two basic layout strategies. The first uses absolute positioning to control where visible items are rendered. The second uses relative positioning (with top/left padding to offset for unrendered items).

In both cases, the list abstraction caches some metadata about the size of items once they have been rendered– so that it knows where to position the items that come after them.

Both of these strategies need to handle reflow. For example, changing the width of a list often affects the height of its itesm. Generally speaking, only the "window" of rendered (visible) items are remeasured in this case (because it would be too slow to rerender and remeasure all of the items before). But once a user scrolls backwards (up/left)– the list needs to account for the reflowed sizes. If it didn't, items would appear to jump up or down (depending on the delta between the previous, cached sizes and the new/reflowed sizes).

How the list deals with new sizes

View ReactNative-useEffect-repro.js
import React, {useEffect, useRef, useState} from 'react';
import {Animated, Text} from 'react-native';
// Begin https://unpkg.com/react-native-animation-hooks@1.0.1/build/AnimationHooks.js
export const useAnimatedValue = initialValue => {
const ref = useRef(new Animated.Value(initialValue));
return ref.current;
};
const getInitialValue = config => {
if (typeof config.initialValue !== 'undefined') return config.initialValue;
@bvaughn
bvaughn / 000.md
Last active Mar 18, 2021
react-window itemData -> data props behavior
View 000.md

Related discussion on bvaughn/react-window/issues/85.

Overview

The specific API feature this Gist is exploring is the itemData prop. This prop provides a way for a component to pass "contextual" list data to an item renderer without adding the overhead of using context. In most cases, a single value is passed (e.g. an array/list) like so:

function ComponentThatRendersAListOfItems({ itemsArray, ...rest }) {
  render() {
    // Pass items array to the item renderer component as itemData:
    return (
View App.js
import React, { Component, Fragment, Suspense } from 'react';
import read from './Resource';
// Wait long enough for DevTools to see
const wait = (ms = 1) => {
const startTime = performance.now();
while (performance.now() - startTime < ms) {
// ...
}
};