Skip to content

Instantly share code, notes, and snippets.

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

Dr. Derek Austin DoctorDerek

☀️
Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / Find Unique JavaScript Objects by Object Reference.js
Created December 7, 2020 19:08
Find Unique JavaScript Objects by Object Reference
// By default, Set compares JS object references
const myObject = { hello: "🌞" }
const objectsArray = [myObject, myObject]
const objectsSet = new Set(objectsArray)
const uniqueObjectReferences = Array.from(objectsSet)
// Equivalent to: (using the spread operator)
// const uniqueObjectReferences = [...objectsSet]
console.log(objectsArray)
// Output: [ { hello: "🌞" }, { hello: "🌞" } ]
@DoctorDerek
DoctorDerek / Props Destructuring in a Function's Arguments for Class Component.js
Created December 4, 2020 23:38
Props Destructuring in a Function's Arguments for Class Component
import React, { Component } from "react"
export default class EmojifyComponent extends Component {
constructor(props) {
super(props)
}
render({ name, emoji }) {
return (
<h1>
@DoctorDerek
DoctorDerek / Props Destructuring in a Function's Arguments.js
Last active December 4, 2020 23:34
Props Destructuring in a Function's Arguments
import React from "react"
const EmojifyComponent = ({ name, emoji }) => {
return (
<h1>
{emoji} {name} {emoji}
</h1>
)
}
export default EmojifyComponent
@DoctorDerek
DoctorDerek / Props Destructuring in a Class Component.js
Last active December 9, 2020 00:00
Props Destructuring in a Class Component
import React, { Component } from "react"
export default class EmojifyComponent extends Component {
constructor(props) {
super(props)
}
render() {
const { name, emoji } = this.props
return (
import React from "react"
const EmojifyComponent = (props) => {
return (
<h1>
{props.emoji} {props.name} {props.emoji}
</h1>
)
}
export default EmojifyComponent
@DoctorDerek
DoctorDerek / Props Destructuring in a Function Component.js
Created December 4, 2020 23:23
Props Destructuring in a Function Component
import React from "react"
const EmojifyComponent = (props) => {
const { name, emoji } = props
return (
<h1>
{emoji} {name} {emoji}
</h1>
)
}
export default EmojifyComponent
@DoctorDerek
DoctorDerek / Basic Object Destructuring Example.js
Created December 4, 2020 19:57
Basic Object Destructuring Example
const myObject = { hello: "👋" }
const { hello } = myObject
console.log(hello) // Output: 👋
myObject.hello === hello // true
@DoctorDerek
DoctorDerek / Array Destructuring Example Using the useState Hook.js
Last active December 4, 2020 23:15
Array Destructuring Example Using the useState Hook
const [name, setName] = useState("Stanny Staunton")
// useState returns an array with two elements
// Array destructuring is a briefer syntax for this code:
const arrayFromUseState = useState("Stanny Staunton")
const nameInState = arrayFromUseState[0]
const setNameInState = arrayFromUseState[1]
// The above code is equivalent to:
const [name2, setName2] = arrayFromUseState
@DoctorDerek
DoctorDerek / Pass Props From The Parent Component.js
Created December 4, 2020 19:52
Pass Props From The Parent Component
<ChildComponent name="Stanny Staunton" email="stanDaMan@stan.com" />
@DoctorDerek
DoctorDerek / String with Comma and a Space.js
Created December 1, 2020 19:26
String with Comma and a Space
const arrayToJoin2 = ["😄", "😄", "😄"]
// comma and a space
arrayToJoin2.join(", ")
// Output: "😄, 😄, 😄"