Skip to content

Instantly share code, notes, and snippets.

View onosendi's full-sized avatar

Daniel Lindegren onosendi

View GitHub Profile
// This is what eslint wants
type Foo = Omit<
Bar, 'baz'
>;
type Foo = Omit<
Bar, 'baz'
>;
export default function ContactForm() {
const [sendContactForm] = useSendContactFormMutation();
const handleSubmit = async () => {
const formData = {};
// If I need the error for this component
try {
await sendContactForm(formData).unwrap();
} catch (error) {
<button data-delete-id="1">One</button>
<button data-delete-id="2">Two</button>
<button data-delete-id="3">Three</button>
@keyframes tooltipFadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
body {
padding: 16px;
function queue(arr, item) {
return [...arr.length >= 3 ? arr.slice(1) : arr, item];
}
const a = queue(['one'], 'two');
const b = queue(a, 'three');
const c = queue(b, 'four');
const d = queue(c, 'five');
console.log(d);
<html lang="en">
<head>
<script src="./script.js" defer></script>
</head>
<body>
<div data-id="parent">
<button type="button">Button 1</button>
<button type="button">Button 2</button>
<button type="button">Button 3</button>
function ChildComponent({ callback }) {}
const MemoizedChildComponent = React.memo(ChildComponent);
function ParentComponent() {
const onClick = useCallback(() => {
console.log('Foo');
}, []);
return <MemoizedChildComponent callback={onClick} />;
}
@onosendi
onosendi / 220124-organize-object-into-array.js
Last active January 24, 2022 21:24
`groupBy` function
const o = [
{ name: 'Bobby', group: 1 },
{ name: 'Jimmy', group: 2 },
{ name: 'Tommy', group: 1 },
];
function groupBy(arr, key = 'group') {
const map = arr.reduce((acc, curr) => ({
...acc,
[curr[key]]: [...(acc[curr[key]] || []), curr],
function List({ items }) {
if (!items?.length) {
return <p>Nothing to see here...</p>;
}
return (
<ul>
{items.map((item) => (
<li key={item.id}>
{!!item.image && <img src={item.image} alt={item.imageAlt} />}
const preferredStores=[{"PreferredStoreName":"Stater Bros","PreferredStoreID":3},{"PreferredStoreName":"Ralph's","PreferredStoreID":7}];
const preferredStoreItems=[{"GroceryItemID":3,"Name":"Test","PreferredStoreID":3,"Notes":"Test","CreatedOn":"Apr 30 2021 10:43PM"},{"GroceryItemID":2,"Name":"Baguette","PreferredStoreID":7,"Notes":"","CreatedOn":"Apr 30 2021 10:43PM"},{"GroceryItemID":1,"Name":"Saltines","PreferredStoreID":3,"Notes":"","CreatedOn":"Apr 30 2021 6:26AM"}]
const uniquePreferredStores = [...new Set(preferredStoreItems.map(({ PreferredStoreID }) => PreferredStoreID))];
const z = uniquePreferredStores.reduce((acc, uId) => ({
...acc,
[uId]: acc[uId] || preferredStores.find((store) => store.PreferredStoreID === uId)?.PreferredStoreName,
}), {});