Typed events and refs in React components with Typescript

Based on how you define your event handlers you can either rely on Typescript’s inference or have to supply the types for the handlers.

If you use inline event handlers then you don’t need to provide types and can just write them like you would in a regular React project.

See all
Reflection in Go
reflect.TypeOf() object with various methods that return info about the type of the var varType.Name() not all vars have name (type), for example slices and pointers don’t but a user defined struct like Foo has a name of Foo varType. See all
Typing the props and state of React class based components

Similar to how things are done for functional components in classes we can use a mix of Typescript’s inference and our own interfaces.

See all
Typing the props and state of React functional components

There are multiple approaches to typing the props and here are two of them.

interface ChildProps {
  color: string;
}

export const Child = ({color}: ChildProps) => {
  return <div>{color}</div>
}
import { Child } from './Child'

const Parent = () => {
  return <Child color="fuchsia" />
}

export default Parent
See all
Using Typescript with React

When using Typescript with React you don’t need a lot of changes to your codebase.

First it’s the file extensions, you’d use .tsx for components and .ts for the rest of the files that would normally be .js files.

See all
Enums in Typescript
Enums allow you to create a type with a fixed list of possible values. They are unordered data structures that map keys to values, similar to objects with fixed keys. The values can be numbers or strings. See all
Leveled logging

Logging information should be managed differently in terms of how much detail should be included and what types of logging should be active in which scenario.

See all
The http.Handler interface
type Handler interface { ServeHTTP(ResponseWriter, *Request) } mux.HandleFunc("/snippet/create", createSnippet) will end up as mux.Handle("/snippet/create", HandlerFunc(createSnippet)) We need to convert the regular func we have to a func that implements the Handler interface via HandlerFunc. See all
Transactions

Managing NULL values

Go is not good at managing NULL values in db records.

For example if a field contains a NULL values that is supposed to be converted into a string that will fail. One solution is to change the field you’re scanning into from string to sql.NullString.

See all
Connecting to a database

We’re using a MySQL driver along with the database/sql package from the std library.

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)”

We assign the driver import to _ because main doesn’t actually use it however we need the init of the package to run so that it can register itself with the database/sql package.

See all
Disabling directory listings
There are 2 approaches: have an empty index.html file inside each dir you want to disable listing in find ./ui/static -type d -exec touch {}/index.html \; create a custom implementation of http. See all
Closures for dependency injection

When you have your handlers spread out over multiple packages you can’t use the simple pattern of declaring your handlers as methods against the application struct (I don’t see why at this point => I’m missing something; is it because you can only declare the handlers against the app struct if they’re in the same package?).

See all
ServeMux

The DefaultServeMux

The reason why you can skip creating a mux and register routes with just http.Handle() or http.HandleFunc() is because Go creates a global var where it stores a default mux instance.

See all
Sentinel errors

Sentinel errors are error objects stored in a global variable. They are created using the errors.New function.

See all
Managing configuration

The idiomatic way to manage config vars is with command line flags. While you can use env vars, there’s advantages in using the cli flags:

  1. cli flags can have default values, env vars cannot
  2. cli flags get automatic type conversions (flag,.String(), flag.Int())
  3. they are documented when starting the app with the -help flag
See all
go.mod

go.sum

This file contains the crypto checksums representing the content of the required packages in go.mod.

See all
Javascript blocks parsing

CSS blocks rendering, meaning that while CSS is downloading and the CSSOM is being built no rendering is done on the screen. However the HTML is still being parsed and the DOM is still being constructed.

See all
Package version resolutions
Sometimes different packages we’re using might have a dependency on the same 3rd party package but with different versions. To make things work we can add a resolutions entry to the package. See all
What does it mean that CSS is render blocking and why it matters

Once the browser starts receiving the response of a HTTP request it starts processing its content. As it encounters links to resources it will begin downloading them. However when it encounters CSS stylesheets it’s going to wait to download the file and finish processing it before rendering anything. That means that if the CSS file is large and takes time to download and process, the user will be waiting with nothing to see on the page.

See all
Using WebPageTest - Notes from the book by Marcel Duran; Andy Davies; Rick Viscomi

Finding the right metrics

“For example, think about the last time you read a news article online. As the page loaded, what were you waiting for? The most probable answer is that you were waiting for the text content of the article itself. The DOMContentLoaded event, also reported by WebPageTest, is like the load event except that it doesn’t wait for images to be displayed. The timing of this event may be a more appropriate metric to track because the time to load the ancillary images should not necessarily be taken into account. The default metric is not always the most relevant to the page in test.”

See all
Javascript parsing and compiling - Web performance with Steve Kinney (Frontend Masters)

Stats

  • 100ms is the limit for having the user feel that the system is reacting instantaneously
  • 1s is about the limit for the user’s flow of thought to stay uninterrupted, even if they will notice the delay
  • 10s is about the limit for keeping the user’s attention
  • 1s slowdown resulted 11% fewer page views, 7% less conversions - Aberdeen Group
  • Akamai found that a 2s delay in web page load time increase bounce rates by 103%
  • 53% of users will leave a mobile site if it takes more than 3s to load
  • if you want users to feel like your site is faster than your competitors, you need to be 20% faster for them to notice
See all
Rendering - Web performance with Steve Kinney (Frontend Masters)

The steps to create a web page are: create the DOM, create the CSSOM, merge them into a render tree, calculate the layout based on the render tree and paint the results on the screen.

See all