Skip to content

Instantly share code, notes, and snippets.

View dugagjin's full-sized avatar
🤠

Dugagjin Lashi dugagjin

🤠
View GitHub Profile
@dugagjin
dugagjin / service-workers.md
Created June 26, 2018 13:38 — forked from Rich-Harris/service-workers.md
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.

I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.

Use Canary for development instead of Chrome stable

Chrome 51 has some pretty wild behaviour related to console.log in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.

@dugagjin
dugagjin / Lazy.tsx
Created June 26, 2018 19:57 — forked from donaldpipowitch/Lazy.tsx
Lazy loading a React component with TypeScript 2.2
// this is our imaginary package "react-lazy"
import React, { Component, createElement } from 'react';
import { Redirect } from 'react-router-dom';
import { observer } from 'mobx-react';
import { observable } from 'mobx';
/**
* This is generic module interface. We assume that we can access React components.
*/
export interface FetchedModule {
@dugagjin
dugagjin / info.md
Last active January 7, 2019 10:10
react fetch example

Overview of how React works

I think it would be helpful to start with a quick overview of how React works, and how async rendering will impact class components.

Conceptually, React does work in two phases:

  • The render phase determines what changes need to be made to e.g. the DOM. During this phase, React calls render and then compares the result to the previous render.
  • The commit phase is when React applies any changes. (In the case of React DOM, this is when React inserts, updates, and removes DOM nodes.) React also calls lifecycles like componentDidMount and componentDidUpdate during this phase.

The commit phase is usually very fast, but rendering can be slow. For this reason, async mode will break the rendering work into pieces, pausing and resuming the work to avoid blocking the browser. This means that React may invoke render phase lifecycles more than once before committing, or it may invoke them with