Skip to content

Instantly share code, notes, and snippets.

View slorber's full-sized avatar
🏠
Working from home

Sébastien Lorber slorber

🏠
Working from home
View GitHub Profile
@sibelius
sibelius / usePreloadRoute.tsx
Created February 10, 2020 12:31
usePreloadRoute to preload code or data based on events
import { useCallback } from 'react';
import { matchRoutes, MatchedRoute, RouteConfig } from 'react-router-config';
const prepareMatches = <Params extends { [K in keyof Params]?: string }>(matches: Array<MatchedRoute<Params>>) => {
return matches.map(match => {
const { route, match: matchData } = match;
const prepared = route.prepare ? route.prepare(matchData.params) : {};
const Component = route.component.get();
if (Component == null) {
route.component.load(); // eagerly load
@brunolemos
brunolemos / v1.tsx
Created December 17, 2019 15:40
useWhyDidYouUpdate
import { useEffect, useRef } from 'react'
export function useWhyDidYouUpdate(name: string, props: Record<string, any>) {
const latestProps = useRef(props)
useEffect(() => {
const allKeys = Object.keys({ ...latestProps.current, ...props })
const changesObj: Record<string, { from: any; to: any }> = {}
allKeys.forEach(key => {
@acutmore
acutmore / README.md
Last active January 21, 2024 20:30
Emulating a 4-Bit Virtual Machine in (TypeScript\JavaScript) (just Types no Script)

A compile-time 4-Bit Virtual Machine implemented in TypeScript's type system. Capable of running a sample 'FizzBuzz' program.

Syntax emits zero JavaScript.

type RESULT = VM<
  [
    ["push", N_1],         // 1
    ["push", False],       // 2
 ["peek", _], // 3
@sebmarkbage
sebmarkbage / WhyReact.md
Created September 4, 2019 20:33
Why is React doing this?

I heard some points of criticism to how React deals with reactivity and it's focus on "purity". It's interesting because there are really two approaches evolving. There's a mutable + change tracking approach and there's an immutability + referential equality testing approach. It's difficult to mix and match them when you build new features on top. So that's why React has been pushing a bit harder on immutability lately to be able to build on top of it. Both have various tradeoffs but others are doing good research in other areas, so we've decided to focus on this direction and see where it leads us.

I did want to address a few points that I didn't see get enough consideration around the tradeoffs. So here's a small brain dump.

"Compiled output results in smaller apps" - E.g. Svelte apps start smaller but the compiler output is 3-4x larger per component than the equivalent VDOM approach. This is mostly due to the code that is usually shared in the VDOM "VM" needs to be inlined into each component. The tr

Everything I Know About UI Routing

Definitions

  1. Location - The location of the application. Usually just a URL, but the location can contain multiple pieces of information that can be used by an app
    1. pathname - The "file/directory" portion of the URL, like invoices/123
    2. search - The stuff after ? in a URL like /assignments?showGrades=1.
    3. query - A parsed version of search, usually an object but not a standard browser feature.
    4. hash - The # portion of the URL. This is not available to servers in request.url so its client only. By default it means which part of the page the user should be scrolled to, but developers use it for various things.
    5. state - Object associated with a location. Think of it like a hidden URL query. It's state you want to keep with a specific location, but you don't want it to be visible in the URL.
@danieldunderfelt
danieldunderfelt / MdxContent.js
Created July 10, 2019 05:49
MDX in React-native
// Use your MDX content with this component.
import React from 'react'
import MDX from '@mdx-js/runtime'
import components from '../utils/markdown/markdown'
// Renders a cimple loading spinner as a test
import Loading from './Loading'
const mdxComponents = {
import {
Dimensions,
LayoutChangeEvent,
Platform,
ScrollEvent,
ScrollView,
StyleSheet,
View,
} from "react-native";
import {Font, Space} from "../atoms";
@gaearon
gaearon / uselayouteffect-ssr.md
Last active May 16, 2024 14:13
useLayoutEffect and server rendering

If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.

You might see a warning if you try to useLayoutEffect on the server. Here's two common ways to fix it.

Option 1: Convert to useEffect

If this effect isn't important for first render (i.e. if the UI still looks valid before it runs), then useEffect instead.

function MyComponent() {
@tkrotoff
tkrotoff / FrontendFrameworksPopularity.md
Last active May 23, 2024 05:29
Front-end frameworks popularity (React, Vue, Angular and Svelte)
@swyxio
swyxio / 1.md
Last active February 8, 2024 22:30
Learn In Public - 7 opinions for your tech career

2019 update: this essay has been updated on my personal site, together with a followup on how to get started

2020 update: I'm now writing a book with updated versions of all these essays and 35 other chapters!!!!

1. Learn in public

If there's a golden rule, it's this one, so I put it first. All the other rules are more or less elaborations of this rule #1.

You already know that you will never be done learning. But most people "learn in private", and lurk. They consume content without creating any themselves. Again, that's fine, but we're here to talk about being in the top quintile. What you do here is to have a habit of creating learning exhaust. Write blogs and tutorials and cheatsheets. Speak at meetups and conferences. Ask and answer things on Stackoverflow or Reddit. (Avoid the walled gardens like Slack and Discourse, they're not public). Make Youtube videos