Skip to content

Instantly share code, notes, and snippets.

@codenickycode
codenickycode / ipv4.bash
Created December 30, 2022 22:21
[CLI: IPv4 Address] CLI command to get your IPv4 address #cli #bash #ip
# MacOs
ipconfig getifaddr en0
# Windows
ipconfig /all
# Linux
hostname -I
@codenickycode
codenickycode / docker-cli.bash
Last active January 2, 2023 19:43
[Docker: CLI] Common Docker CLI Commands #docker #cli
# List all running containers
docker ps
# List all running and stopped containers
docker ps -a
# List all local images
docker images
# List all volumes
@codenickycode
codenickycode / Dockerfile
Created January 3, 2023 13:38
[Docker: Dockerfile] Syntax to build an image #docker
# Argument outside the build stage
# To be used in first FROM
ARG <key>=<value>
# Parent image
FROM [--platform=<platform>] <image> [AS <name>]
# or
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
# or
FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
@codenickycode
codenickycode / react-state-management.md
Created January 3, 2023 17:24
[React: State Management Cheat Sheet] A rule of thumb for state management in React #react #state-management

Adapted from RemcoProgrammer's answer here:

Use Case Solution Example
Backend Cache React Query, Apollo Client fetch response
Navigation State React Router URL, params, history state
Component State (and subtree) useState, custom hooks is this thing currently open, what are the form contents and errors, what page of the table are we looking at
Unchanging Global State useContext auth, theme, language choice
Changing Global State Redux, Zustand dashboard data
@codenickycode
codenickycode / path.js
Last active January 29, 2023 18:56
[Node.js: Path Resolution] path module resolution #node #javascript #path #url #module #import
import path from 'path';
import { fileURLToPath } from 'url';
const currentModuleURL = import.meta.url;
const pathToFile = fileURLToPath(currentModuleURL);
console.log(pathToFile);
//=> /Users/nick/Repos/node-test/path.js
console.log(path.dirname(pathToFile));
@codenickycode
codenickycode / eslint-vs-prettier.md
Last active January 29, 2023 18:53
[React: ESLint vs. Prettier] React docs best practice when ESLint conflicts with Prettier #eslint #lint #prettier #formatting #react
@codenickycode
codenickycode / react-dont-nest-components.md
Last active January 30, 2023 15:51
[React: Don't nest component definitions] React docs warning about nested component definitions #react #components #nesting
@codenickycode
codenickycode / react-rules-of-keys.md
Last active January 29, 2023 19:34
[React: Rules of keys] Must do's for keys when rendering array items #react #keys

https://beta.reactjs.org/learn/rendering-lists#rules-of-keys

Rules of keys

  • Keys must be unique among siblings. However, it’s okay to use the same keys for JSX nodes in different arrays.
  • Keys must not change or that defeats their purpose! Don’t generate them while rendering.

Pitfall

You might be tempted to use an item’s index in the array as its key. In fact, that’s what React will use if you don’t specify a key at all. But the order in which you render items will change over time if an item is inserted, deleted, or if the array gets reordered. Index as a key often leads to subtle and confusing bugs.

Similarly, do not generate keys on the fly, e.g. with key={Math.random()}. This will cause keys to never match up between renders, leading to all your components and DOM being recreated every time. Not only is this slow, but it will also lose any user input inside the list items. Instead, use a stable ID based on the data.

@codenickycode
codenickycode / react-before-you-use-context.md
Created January 30, 2023 16:29
[React: Before you use context] Try these before reaching for context #react #context

https://beta.reactjs.org/learn/passing-data-deeply-with-context#before-you-use-context

Before you use context

Context is very tempting to use! However, this also means it’s too easy to overuse it. Just because you need to pass some props several levels deep doesn’t mean you should put that information into context.

Here’s a few alternatives you should consider before using context:

  1. Start by passing props. If your components are not trivial, it’s not unusual to pass a dozen props down through a dozen components. It may feel like a slog, but it makes it very clear which components use which data! The person maintaining your code will be glad you’ve made the data flow explicit with props.
  2. Extract components and pass JSX as children to them. If you pass some data through many layers of intermediate components that don’t use that data (and only pass it further down), this often means that you forgot to extract some components along the way. For example, maybe you pass data props like `posts
@codenickycode
codenickycode / react-ref-callback.tsx
Created January 30, 2023 19:25
[React: Ref callback] Use a ref callback to set a list of nodes #react #ref #useRef #dom
// https://beta.reactjs.org/learn/manipulating-the-dom-with-refs#how-to-manage-a-list-of-refs-using-a-ref-callback
// in the function body
const itemsRef = useRef(null);
function getMap() {
if (!itemsRef.current) {
// Initialize the Map on first usage.
itemsRef.current = new Map();
}