Skip to content

Instantly share code, notes, and snippets.

View DoctorDerek's full-sized avatar
☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com

Dr. Derek Austin DoctorDerek

☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / recommended-settings.json
Last active March 31, 2024 07:31
Recommended settings.json file for new web developers setting up VS Code according to https://medium.com/p/65aaa5788c0d/ by Dr. Derek Austin 🥳
@DoctorDerek
DoctorDerek / ParentComponent.jsx
Last active November 6, 2023 16:54
ParentComponent.jsx - How to Pass All Props to a Child Component in React
import React from "react"
import DisplayAllProps from "./DisplayAllProps"
import ChildComponent from "./ChildComponent"
const ParentComponent = (props) => (
<section>
<h1>ParentComponent's props:</h1>
<DisplayAllProps {...props}></DisplayAllProps>
<ChildComponent {...props}></ChildComponent>
@DoctorDerek
DoctorDerek / How to Check for a Symbol in JavaScript.js
Last active April 25, 2023 02:25
How to Check for a Symbol in JavaScript
// Every symbol created with Symbol() is unique.
console.log(Symbol() === Symbol()) // false
console.log(Symbol("✨") === Symbol("✨")) // false
// Calling Symbol.for() makes a global symbol.
console.log(Symbol.for("✨") === Symbol.for("✨")) // true
// You can check for a symbol using typeof.
console.log(typeof Symbol()) // "symbol"
Array size: 10 items 1,000 items 100,000 items 1,000,000 items
.includes() Fastest Fastest 47.07% slower 46.77% slower
.filter() 97.16% slower 99.99% slower 97.69% slower 97.66% slower
.some() 84.81% slower 99.98% slower 95.11% slower 95.01% slower
for loop 84.2% slower 99.62% slower Fastest Fastest
.find() 85.74% slower 99.98% slower 95.19% slower 95.06% slower
@DoctorDerek
DoctorDerek / One-Liner to Use Set with an Array of Objects Unique Objects.js
Created November 4, 2020 19:57
Easy One-Liner to Use Set to Find Unique Objects within an Array of Objects by the Objects' Contents
const uniqueObjectsOneLiner = [
...new Set(objectsArray.map((o) => JSON.stringify(o))),
].map((string) => JSON.parse(string))
console.log(`${uniqueObjectsOneLiner.length} objects`)
// Output: 2 objects
console.log(...uniqueObjectsOneLiner)
// [ { id: 1, emoji: "🎸" }, { id: 2, emoji: "🎷" } ]
// Export default const in JavaScript and TypeScript
const Banana = () => (<div>🍌</div>)
export default Banana // Has to be on a separate line
// Default exports in JavaScript and TypeScript
function Banana() {
return <div>🍌</div>
}
const price = 1.33
// In this example, price is a named export:
export { Banana as default, price }
// Named exports in JavaScript and TypeScript
function Banana() {
return <div>🍌</div>
}
const price = 1.33
export { Banana, price }
// These are equivalent to the export syntax above:
@DoctorDerek
DoctorDerek / type-ReactNode.tsx
Created October 21, 2022 17:45
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
type ReactNode =
| ReactElement
| string
| number
| ReactFragment
| ReactPortal
| boolean
| null
| undefined
@DoctorDerek
DoctorDerek / right-type-for-children-with-type-instead-of-interface.tsx
Created October 21, 2022 17:44
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
import { ReactNode } from "react"
type ILayout = {
pageInfo?: { name: string; description: string }
children: ReactNode
}
export default ILayout