Skip to content

Instantly share code, notes, and snippets.

View jamesknelson's full-sized avatar

James K Nelson jamesknelson

View GitHub Profile

mapObservableToChunkIterator(observable, mapValueToChunks, mapErrorToChunks?): Generator<Promise | Chunk[]>

A function that accepts an Observable with a subscribe() method, and optionally with a getCurrentValue() or getCurrentResult() method.

It returns a Generator that yields Promise (when waiting for a new value) or Chunk objects (i.e. objects that extend { type: string }).

By default, mapErrorToChunks takes the error emitted by an observable, and yields [{ type: 'error', error: error }].

When the observable is complete, the Generator's next() function will return { done: true }.

@jamesknelson
jamesknelson / component-routing.js
Last active March 19, 2019 11:22
Two APIs for routing with React that support POST methods and SSR.
/**
UPDATE:
This component and hook based routing/fetching API won't work, as `useAsync()`
is an impossible component.
In order to use async functions to respond to route changes, the functions will
need to be registered with a parent cache/provider with a unique key. As such, a
more natural component-based architecture would involve a `<Route path>` component
import { map, redirect, route } from 'navi'
function UpdatePasswordForm({ submitErrors }) {
return (
<Form
method='post'
submitErrors={submitErrors}
validate={value =>
value.password !== value.passwordConfirmation &&
{
@jamesknelson
jamesknelson / gist:432f00af5522ea07cbb39990a8105e0c
Created January 17, 2019 08:50
The Hierarchy (for React apps)
A React app can be split into a number of different types of functions and components, where *each type can depend only on the types above it in the hierarchy*.
This makes gives you a way to split up components and functions over your filesystem. It also helps you to keep components from growing too large, and encourages practices that make testing easier.
```
types (TypeScript typings)
^
utils (plain javascript functions)
^
contexts (React context, and provider components)
@jamesknelson
jamesknelson / create-cruv-app.sh
Last active August 22, 2018 02:11
React CRUV - 4 directories to solve project structure for good
create-react-app myapp
cd myapp/src
mkdir containers routes utils views
touch config.js contexts.js
mv App.* routes
sed -i '' -e 's/.\/App/.\/routes\/App/g' index.js
// typeof DocumentStorage
type DocumentStorage = Batchable<{
willUpdate: false | {
source: boolean | CompoundValueNode<boolean>;
undoHistories: boolean | CompoundValueNode<boolean>;
};
isAwaitingInitialData: false | {
source: boolean | CompoundValueNode<boolean>;
undoHistories: boolean | CompoundValueNode<boolean>;

I want to make a <TextFieldControl> component that passes a <Description> component to its render function.

Rendering a <Description> would automatically add an id to the element, and then add that id to the aria-describedby prop of the associated input.

For example, this component:

const EmailField = ({ error, value, onChange }) =>
import * as Govern from 'govern'
/**
* This component subscribes to a Redux store, outputting its current state.
*
* Typically, you'll use this component as an element in another component's
* `subscribe()` method. For example:
*
* ```js
* class Identity extends Govern.Component {
import React, { PureComponent, PropTypes } from "react"
import Prism from 'prismjs'
const codeBlockAliases = {
'js': 'jsx',
'html': 'markup',
'mdx': 'markdown',
'md': 'markdown',
}
@jamesknelson
jamesknelson / Pitch.mdx
Last active February 3, 2017 08:39
mdx: A markdown-like format that gives you inline JSX, and compiles to a React stateless function
metaDescription
junctions.js lets you add routes to your React components.

import Link from './Link' import ButtonTheme from './ButtonTheme' import ExampleSet from './ExampleSet' import ComponentExample from './ComponentExample' import GistVanillaExample from './examples/the-gist-vanilla' import GistSugarFreeExample from './examples/the-gist-sugar-free' import BasicExample from './examples/basic'