Skip to content

Instantly share code, notes, and snippets.

@radix
radix / childState.jsx
Last active March 31, 2023 05:21
useSelectiveState / localizing state access to child components to avoid unnecessary re-renders
import React, { useState } from 'react';
/*
It's common to be in a situation where your react component renders a bunch of children,
and only one of those children rely on some state your component keeps. You don't want
to re-render ALL of your children just because you need to keep some state relevant to
only one of them, so the natural refactoring is to localize that state to the one child.
But... what if your parent (or one of the other children) is actually responsible for
*setting* that state, or maybe you just have a couple of disparate components that use
that state, while most of them don't touch it?
@radix
radix / traceback.txt
Last active January 4, 2019 18:34
structlog colorama ansitowin32 traceback in ECS
File "/venv/local/lib/python2.7/site-packages/structlog/_base.py", line 192, in _proxy_to_logger
return getattr(self._logger, method_name)(*args, **kw)
File "/venv/local/lib/python2.7/site-packages/structlog/_loggers.py", line 76, in msg
until_not_interrupted(self._write, message + "\n")
File "/venv/local/lib/python2.7/site-packages/structlog/_utils.py", line 24, in until_not_interrupted
return f(*args, **kw)
File "/venv/local/lib/python2.7/site-packages/colorama/ansitowin32.py", line 41, in write
self.__convertor.write(text)
File "/venv/local/lib/python2.7/site-packages/colorama/ansitowin32.py", line 154, in write
self.write_and_convert(text)
@radix
radix / gist:7b238da0388f8a90f347ca2588199ac6
Created February 16, 2018 20:22
rustfmt test failure
Mismatch at tests/source\file-lines-6.rs:8:
fn bar() {⏎
// test⏎
let i = 12;⏎
+⏎
// test⏎
}⏎
ERROR in ./src/typescript/PlayerView.tsx
(22,10): error TS2322: Type '{ children: Element[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<TabbedView> & Readonly<{ children?: ReactNode; }> ...'.
Type '{ children: Element[]; }' is not assignable to type 'Readonly<TabbedViewProps>'.
Types of property 'children' are incompatible.
Type 'Element[]' is not assignable to type 'ComponentElement<any, Tab>[]'.
Type 'Element' is not assignable to type 'ComponentElement<any, Tab>'.
Types of property 'type' are incompatible.
Type 'string | ComponentClass<any> | StatelessComponent<any>' is not assignable to type 'ComponentClass<any>'.
Type 'string' is not assignable to type 'ComponentClass<any>'.
let scene = this.props.currentScene;
let sceneButton;
if (scene && players[pid].scene !== scene) {
sceneButton = <button onClick={() => {this.props.onAddToScene(pid, scene)}}>Add to scene</button>;
} else {
sceneButton = <button onClick={() => this.props.onRemoveFromScene(pid)}>Remove</button>;
}
@radix
radix / ElmBug.elm
Created February 3, 2017 22:53
Elm bug: `Cannot read property 'tag' of undefined`
module ElmBug exposing (..)
import Json.Decode as JD
import Json.Helpers as JH
import Dict
sumDecoder : String -> List (String, a) -> List (String, JD.Decoder a) -> JD.Decoder a
sumDecoder name nullaryList singleFieldList = JD.oneOf
[ JH.decodeSumUnaries name
impl<A> Creature<A> {
pub fn into_vari(self) -> CreatureVari {
if self.can_act() {
CreatureVari::Able(Creature::<Able> {
name: self.name,
pos: self.pos,
conditions: self.conditions,
abilities: self.abilities,
cur_energy: self.cur_energy,
cur_health: self.cur_health,
@radix
radix / Shakefile.hs
Last active May 23, 2016 14:32
Nixy Shake
{-# LANGUAGE TupleSections #-}
import Control.Monad (when, foldM_, forM)
import Data.Functor
import Data.List
import Data.Ord (comparing)
import Development.Shake
import Development.Shake.FilePath
import qualified System.Directory as Dir
import System.Posix.Files (touchFile)
@radix
radix / fp_flask.py
Last active November 25, 2015 21:33
A purely functional microwrapper for Flask, using Effect (https://effect.readthedocs.org/)
"""
A microwrapper for Flask that gives you a pure-functional API.
- Request objects are passed to your handlers, instead of relying on the global
``flask.request``.
- Route handlers can return Effect objects (see https://effect.readthedocs.org/),
so you can represent your side-effects as pure values.
"""
from functools import wraps
@radix
radix / specific-loadbalancer-node.xml
Last active October 7, 2015 17:26
/{tenant_id}/loadbalancers/{lb_id}/nodes/{node_id}.atom
<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom"><link rel="next" href="https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/756004/loadbalancers/493411/nodes/1007563.atom?page=2" /><title type="text">Node Feed</title><id>756004-loadbalancers-493411-nodes-1007563</id><author><name>Rackspace Cloud</name></author><entry><title type="text">Node Successfully Updated</title><summary type="text">Node successfully updated with address: '10.181.30.33', port: '80', weight: '1', condition: 'DRAINING'</summary><author><name>radix192;q=1.0</name></author><link href="https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/756004/loadbalancers/493411/nodes/1007563" /><id>756004-loadbalancers-493411-nodes-1007563-2015280343480</id><category term="UPDATE" /><updated>2015-10-07T03:43:48.000Z</updated></entry><entry><title type="text">Node Successfully Created</title><summary type="text">Node successfully created with address: '10.181.30.33', port: '80', condition: 'ENABLED', weight: '1'</summary>