Skip to content

Instantly share code, notes, and snippets.

@brigand
brigand / a.example.md
Last active May 28, 2019 04:28
A react hook for an enum-like interface to async data

Simple example where .match is used to render in one of three states.

function User({ id }) {
  const state = usePromise(() => get('/api/user', { id }), [id]);
  
  return (
    <div className={css.root}>
      {state.match({
@brigand
brigand / .js
Last active April 11, 2019 17:18
react-router - clicking a link again refreshes data
import useReactRouter from 'use-react-router';
function UserPage({ match }) {
const [user, setUser] = React.useState(null);
const { history } = useReactRouter();
// If on /user/123 and you click a link to /user/123, history.length will be
// increased by 1, causing the effect to run again
React.useEffect(() => {
let cancel = false;
@brigand
brigand / 1.example.js
Last active March 26, 2019 18:32
The `useLocationState` hook gives a `React.useState`-like interface to updating the HTML5 History API location state.
function Foo() {
const [count, setCount] = useLocationState('Foo', 0);
return (
<div>
<button onClick={() => setCount(c => c + 1)}>Clicks: {count}</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
);
}
@brigand
brigand / example.js
Last active February 27, 2019 06:36
React Hook 'useDebounced' for debouncing event handlers
function Foo({ onClick }) {
let handler = useDebounced(onClick, { delay: 1000, leading: true });
return <button onClick={handler}>Click me</button>;
}
import React, { Component } from 'react';
import styled from 'styled-components';
const Figure = styled.figure`
height: 0;
margin: 0;
background-color: #efefef;
position: relative;
padding-bottom: ${props => props.ratio}%;
`;
@brigand
brigand / .md
Created December 30, 2018 05:35
Issues with the KM Resizer Tool (post here!)

Issues with the KM Resizer Tool

It's a tiny project, so please leave any error reports as comments here.

@brigand
brigand / .jsx
Created November 29, 2018 22:42
Accessible Custom Radio Input for React
const defaultItemRender = (props, active, children) => (
<FlexCol className={[s.label, active && s.active]} {...props}>
{children}
</FlexCol>
);
export default class RadioSet extends React.Component<Props> {
baseId = `Radio-${++idCounter}`;
@brigand
brigand / a.styled.jsx
Last active November 6, 2019 05:39
Basic implementation of !important styles for React
class Styled extends React.Component {
static counter = 1e9;
id = `__styled__${Styled.counter++}`;
componentDidMount() {
this.apply();
}
componentDidUpdate(prevProps, prevState) {
const anyChanged = Object.entries({
@brigand
brigand / apiMiddleware.js
Created June 17, 2018 12:00
Custom redux api middleware. Take and modify to fit your needs.
/*
Example action:
{
type: 'MY_API',
payload: {
method: 'POST',
path: 'foo/:userId',
params: { userId: '123' },
query: { my_query: 1 },
body: { my_body: 2 },
@brigand
brigand / .md
Last active April 1, 2018 03:10
Ctx (the invisible argument)

Input Code

Looks like mostly normal JS code. It keeps a stack of names for each time .__sub__ctx__ is called. The output of this program is "value: 10, stack: foo->bar->baz".

You could use this technique to e.g. pass a log function around that's specific to an HTTP request, or a database client, or implement a hierarchy of event emitters.

function makeCtx() {
  'disable ctx';