Skip to content

Instantly share code, notes, and snippets.

@bengrunfeld
bengrunfeld / graphql-data.js
Created June 21, 2020 08:21
GraphQL-Apollo-NextJS Starter App: pages/api/graphql-data.js
import { ApolloServer, gql } from "apollo-server-micro";
let book = {
name: "The Hungarian Sausage",
author: "Ben Grunfeld",
};
const typeDefs = gql`
type Book {
name: String
import { useRouter } from 'next/router'
const Post = ({ post }) => {
const router = useRouter()
if (router.isFallback) {
return <div>Loading...</div>
}
return (
@bengrunfeld
bengrunfeld / toolbox.js
Last active July 26, 2020 13:54
Toolbox of Useful Code
// Find the min of an array
const a = [3, 8, 2, 99, 1, 42, 23]
Math.min.apply(Math, a)
// How to turn an array into an object
const a = [3, 8, 2, 99, 1, 42, 23]
const b = a.reduce((a, b, i) => {
const obj = {}
obj[i] = b
@bengrunfeld
bengrunfeld / without-useCallback-1.jsx
Last active July 26, 2020 11:58
Passing functions as props causes unnecessary re-renders
import { useState } from "react";
const CountDisplay = ({ count }) => <h3>Count: {count}</h3>;
const CountButton = ({ updateCount }) => {
// Note that this line will fire every time,
// showing us that an unneeded re-render has occurred.
console.log("=> CountButton render", Date.now());
return <button onClick={() => updateCount()}>Increment</button>;
@bengrunfeld
bengrunfeld / useCallback-example.js
Created July 26, 2020 12:03
useCallback helps us prevent unnecessary re-renders
import { useState, useCallback } from "react";
const CountDisplay = ({ count }) => <h3>Count: {count}</h3>;
const CountButton = React.memo(({ updateCount }) => {
// Note that this line will now only execute once on initial render!
console.log("=> CountButton render", Date.now());
return <button onClick={() => updateCount()}>Increment</button>;
});
@bengrunfeld
bengrunfeld / without-useMemo.jsx
Last active July 26, 2020 14:18
Data Processing without useMemo
import { useState, useMemo } from "react";
const data = [
[93, 64, 60, 28, 17, 24, 36, 9, 47, 11, 80, 45],
[87, 76, 66, 20, 18, 71, 5, 22, 36, 48, 28, 99],
[3, 71, 62, 59, 22, 15, 33, 19, 13, 98, 98, 42],
];
const double = (arr) => {
// This shows that the function is being run on each render
@bengrunfeld
bengrunfeld / with-useMemo.jsx
Last active July 26, 2020 14:18
Data Processing with useMemo
import { useState, useMemo } from "react";
const data = [
[93, 64, 60, 28, 17, 24, 36, 9, 47, 11, 80, 45],
[87, 76, 66, 20, 18, 71, 5, 22, 36, 48, 28, 99],
[3, 71, 62, 59, 22, 15, 33, 19, 13, 98, 98, 42],
];
const double = (arr) => {
console.log("=> double", Date.now());
@bengrunfeld
bengrunfeld / useImperativeHandle.jsx
Last active July 29, 2020 08:05
Example usage of useImperativeHandle
import { useState, useRef, forwardRef, useImperativeHandle } from "react";
const ChildOne = forwardRef((props, ref) => {
const [count, setCount] = useState(0);
useImperativeHandle(ref, () => ({
count,
}));
const updateCount = () => {
@bengrunfeld
bengrunfeld / Dockerfile
Last active August 2, 2020 17:49
A Node based Dockerfile
FROM node:12.18.3
WORKDIR /usr/src/app
ENV MESSAGE "Hello from Ben"
COPY package*.json ./
RUN npm install
@bengrunfeld
bengrunfeld / docker-compose.yml
Created August 2, 2020 17:12
Docker Compose example
version: "2"
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: web
ports:
- "4000:4000"