Skip to content

Instantly share code, notes, and snippets.

usePersistedState hook for Next.js

Here is the code for a hook that wraps the useState hook and persistes the value in a cookie. There are lots of other implementations of a similar hook (e.g. https://dev.to/selbekk/persisting-your-react-state-in-9-lines-of-code-9go) but they don't work with Next.js because localstorage is not available on the server.

Usage

import React from "react";

import usePersistedState from "./usePersistedState";
@jkimbo
jkimbo / test_custom_fields.py
Last active April 7, 2021 07:09
Strawberry custom fields
from textwrap import dedent
from typing import Any, Awaitable, Dict, Type, Union, cast, List
import strawberry
from strawberry.field import StrawberryField
from strawberry.arguments import StrawberryArgument, UNSET
from strawberry.types.generics import get_name_from_types
def test_simple_custom_field():
@jkimbo
jkimbo / extensions.py
Created May 23, 2021 08:29
Strawberry SentryPerformanceExtension
import logging
from typing import Optional, Literal, List, cast
import sentry_sdk
from graphql import DocumentNode as GraphQLDocumentNode
from graphql.language import OperationDefinitionNode, DefinitionNode
from strawberry.extensions import Extension
logger = logging.getLogger(__name__)
import logging
from typing import Optional, Literal, List, cast
from graphql import DocumentNode as GraphQLDocumentNode
from graphql.language import OperationDefinitionNode, DefinitionNode
from strawberry.extensions import Extension
def get_first_operation(
#!/usr/bin/env bash
if [ -z "$1" ]; then
echo "Diff or diff range not provided"
exit 1
fi
diff_range=$1
shift

Keybase proof

I hereby claim:

  • I am jkimbo on github.
  • I am jkimbo (https://keybase.io/jkimbo) on keybase.
  • I have a public key whose fingerprint is EC6E BD70 7A23 E151 0881 327F CCF1 7088 099B F097

To claim this, I am signing this object:

<iframe style="width: 100%;" id="fronted-payments-container" src="https://app.fronted.rent/embed/v1/widget?partnerId=..." scrolling="no" />
<script>
(function() {
var frame = document.getElementById("fronted-payments-container");
frame.addEventListener("load", function() {
frame.contentWindow.postMessage("measure-height", "https://app.fronted.rent");
});
window.addEventListener("message", function(event) {
if (event.origin !== "https://app.fronted.rent") return;
var data = JSON.parse(event.data);
@jkimbo
jkimbo / relay.md
Last active April 8, 2023 15:05
Strawberry Relay API

Strawberry Relay API

Proposal for the Strawberry Relay API.

Node

from strawberry import relay

@strawberry.type