Skip to content

Instantly share code, notes, and snippets.

View itaditya's full-sized avatar
🎯
Focusing

Aditya Agarwal itaditya

🎯
Focusing
View GitHub Profile
@itaditya
itaditya / positional-args.js
Created August 1, 2020 14:29
(Blog) Why named arguments are better than positional arguments
function greet(firstName, lastName) {
console.log(`Hello ${firstName} ${lastName}`);
}
// intended usage
greet('Michael', 'Scott');
const fName = 'Harry';
const lName = 'Potter';
@itaditya
itaditya / buttons-api.jsx
Last active July 11, 2020 17:41
Blog- Composing buttons in design systems with Base + Variant pattern
function ButtonBase(props) {
const {
className,
size = 'base',
as = 'button', // let Ana render it as a button or an anchor.
children,
IconStart, // use to render an icon before children
IconEnd, // use to render an icon after children
...restProps
} = props;
@itaditya
itaditya / machine.js
Created June 25, 2020 00:57
Generated by XState Viz: https://xstate.js.org/viz
function canGoToAddressTab(context, event) {
if (!context.dataBooks || context.dataBooks.length < 1) {
return false;
}
return true;
}
function canGoToPaymentTab(context, event) {
@itaditya
itaditya / changes.patch
Last active June 20, 2020 19:43
Blog- Optimizing React apps with Memo.
--- a/src/App.js
+++ b/src/App.js
-import React, { useState } from 'react';
+import React, { useState, useMemo } from 'react';
import cn from 'clsx';
function Table(props) {
console.log('render: Table');
// other old code
}
@itaditya
itaditya / strict-equality.js
Created June 10, 2020 20:33
Blog- Optimizing React apps with Memo
const num1 = 3;
const num2 = 7;
const num3 = 3;
console.log(num1 === num2); // false
console.log(num1 === num3); // true
const arr1 = [1];
const arr2 = [1];
@itaditya
itaditya / changes.patch
Last active June 11, 2020 20:51
Blog- Optimizing React apps with Memo
--- a/src/App.js
+++ b/src/App.js
function Table(props) {
console.log('render: Table');
// other old code
}
+const OptimizedTable = React.memo(Table);
function App() {
@itaditya
itaditya / App.jsx
Last active June 10, 2020 19:56
Blog- Optimizing React apps with Memo
import React, { useState } from 'react';
import cn from 'clsx';
import { housesData, charactersData } from './staticData';
import './styles.css';
function Header(props) {
const { children } = props;
return (
<header className="app-header">
@itaditya
itaditya / shallow-compare.js
Last active June 9, 2020 21:30
Blog- Optimizing React apps with Memo
const car1 = {
color: 'red',
model: 'S',
};
const car2 = {
color: 'red',
model: 'X',
};
@itaditya
itaditya / func-compare.js
Last active June 7, 2020 13:27
Blog- Optimizing React apps with Memo
function funcObj() {
return {
ans: 42,
};
}
function funcArr() {
return [42];
}
@itaditya
itaditya / script.js
Created May 18, 2020 22:49
Trigger event from JS such that React can listen to them
function setNativeValue(element, value) {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}