Skip to content

Instantly share code, notes, and snippets.

View drumnickydrum's full-sized avatar

Nick Carbone drumnickydrum

View GitHub Profile
@drumnickydrum
drumnickydrum / pyenv.sh
Created June 12, 2023 17:21
[Managing Python Env] Install pyenv and setup shell to manage versions #python #env
# Install pyenv, to manage Python versions
brew update
brew install pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
# Install the supported version of Python, as defined in .python-version
pyenv install
@drumnickydrum
drumnickydrum / deferredPromise.ts
Last active April 19, 2023 21:57
[TS: Deferred Promise] Create a promise and pass along its res and rej #typescript #javascript #promise
// Andrew Burgess, YouTube
// https://www.youtube.com/watch?v=Yvhad4zdPqI
export class Deferred<T, E = unknown> {
promise: Promise<T>;
resolve: (value: T) => void = () => null;
reject: (reason?: E) => void = () => null;
constructor() {
this.promise = new Promise({resolve, reject} => {
@drumnickydrum
drumnickydrum / rxjs-higher-order-maps.md
Last active February 10, 2023 14:32
[RxJS: Higher-Order Maps] Use cases for most common higher-order map operators #rxjs #angular #operators

From Reactive Patterns for RxJS with Angular Chapter 7: Transforming Streams

Operator Use Case
concatMap If the order is important and you need to process operations in sequence while waiting for completion.
mergeMap If the order is not important and you need to process operations in parallel to enhance performance.
switchMap If you need to put a cancellation logic to release resources and take always the most recent information.
exhaustMap To ignore new observables while the current one is still ongoing.
@drumnickydrum
drumnickydrum / rxjs-observable-vs-promise.md
Last active February 6, 2023 22:58
[RxJS: Observable vs Promise] Observable vs. Promise Cheat Sheet #rxjs #observable #promise #angular

https://v12.angular.io/guide/comparing-observables#cheat-sheet

OPERATION OBSERVABLE PROMISE
Creation new Observable((observer) => { observer.next(123); }); new Promise((resolve, reject) => { resolve(123); });
Transform obs.pipe(map((value) => value * 2)); promise.then((value) => value * 2);
Subscribe sub = obs.subscribe((value) => { console.log(value) }); promise.then((value) => { console.log(value); });
Unsubscribe sub.unsubscribe(); Implied by promise resolution.
@drumnickydrum
drumnickydrum / rxjs-observable-operator-pipeline.md
Created February 6, 2023 22:36
[RxJS: Observable Operator Pipeline] Create an observer with an operator pipeline #rxjs #observable #angular

Angular Docs: Rx Library | Operators

import { of } from 'rxjs';
import { filter, map } from 'rxjs/operators';

const squareOdd = of(1, 2, 3, 4, 5)
  .pipe(
    filter(n => n % 2 !== 0),
 map(n =&gt; n * n)
@drumnickydrum
drumnickydrum / 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();
}
@drumnickydrum
drumnickydrum / 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
@drumnickydrum
drumnickydrum / 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.

@drumnickydrum
drumnickydrum / 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
@drumnickydrum
drumnickydrum / 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