Skip to content

Instantly share code, notes, and snippets.

View szaranger's full-sized avatar

Sean Amarasinghe szaranger

View GitHub Profile
import { useCallback, useState } from "react";
function Child({ calc }) {
return <p>{`Total: ${calc()}`}</p>;
}
export default function Parent() {
const [a, setA] = useState(0);
const [b, setB] = useState(0);
function calc(x, y) {
return x + y;
}
const memoizedCalc = useCallback(() => {
return calc(a, b);
}, [a, b]);
let initialState;
class User {
#isActive = false;
get getState(){
if(!this.#isActive){
throw new Error('User is not active');
}
return this.#isActive;
class User {
isActive = false;
get getStatus(){
if(!this.#isActive){
throw new Error('User is not active');
}
return this.#isActive;
}
}
let users;
if (userType === 'ADMIN') {
users = await import('https://example.com/users/1');
} else {
users = await import('https://example.com/users/2');
}
const date = Temporal.Now.plainDateISO(); // Gets the current date
date.toString(); // returns the date in ISO 8601 date format
// If you additionally want the time:
Temporal.Now.plainDateTimeISO().toString(); // date and time in ISO 8601 format
try {
const result = 2 / 0;
console.log(result);
} catch (error) {
console.err(error);
throw new Error('Something went wrong', { cause: error });
}
const fruits ="Fruits:apples, oranges, pears";
const regex = /(apples)/gd;
const matches = [...fruits.matchAll(regex)];
console.log(matches[0]);
const person = {
hasOwnProperty: function() {
return false;
},
age: 21
};
if (Object.hasOwn(person, 'age')) {
console.log(person.age); // true - the remplementation of hasOwnProperty() did not affect the Object
}