Skip to content

Instantly share code, notes, and snippets.

View jamesseanwright's full-sized avatar
🤔

James Wright jamesseanwright

🤔
View GitHub Profile
@jamesseanwright
jamesseanwright / axios-hoc.jsx
Created March 12, 2019 16:26
A higher-order component to deduplicate Axios boilerplate
const withGet = Component =>
class AugmentedWithGet extends React.Component {
get = url => {
this.setState({
isLoading: true,
});
axios.get(url)
.then(({ data, status }) => {
this.setState({
import * as React from 'react';
export default () => (
<section>
<h2>Bacon</h2>
<p>
Bacon ipsum dolor amet kielbasa swine jerky, beef ribs sausage turducken
short ribs strip steak venison buffalo meatball tongue. T-bone short loin
frankfurter capicola buffalo. Kevin ham hock chuck tail kielbasa short
@jamesseanwright
jamesseanwright / react-client-router-usage.tsx
Last active July 15, 2019 12:51
Example usage of custom client-side router for React
import { Router } from './routing';
import Nav from './Nav.jsx';
import * as pages from './pages';
const routes = new Map<string, React.ComponentType>([
['/', () => <p>Pick an Ipsum!</p>],
['/lorem', pages.Lorem],
['/bacon', pages.Bacon],
['/hipster', pages.Hipster],
['/office', pages.Office],
@jamesseanwright
jamesseanwright / example-link-usage.tsx
Created July 15, 2019 12:54
Example usage of Link component
@jamesseanwright
jamesseanwright / react-suspense-example.tsx
Last active July 16, 2019 12:49
React Suspense example
import * as React from 'react';
const App = () => (
<main>
<h1>My App</h1>
<React.Suspense fallback={<p>Loading...</p>}>
<SomeSuspensefulComponent />
</React.Suspense>
</main>
);
import * as React from 'react';
const App = () => (
<main>
<h1>My App</h1>
<React.Suspense fallback={<LoadingSpinner />}>
<Page />
<React.Suspense fallback={<MiniSpinner />}>
<PageMetadata />
</React.Suspense>
@jamesseanwright
jamesseanwright / react-lazy-res-impl.js
Created July 17, 2019 16:21
React lazy resolution state tracking
switch (status) {
case Resolved: {
const Component: T = result;
return Component;
}
case Rejected: {
const error: mixed = result;
throw error;
}
case Pending: {
import * as React from 'react';
const Lorem = React.lazy(() => import('./pages/Lorem'));
const App = () => (
<React.Suspense fallback={<div className="loading-spinner" />}>
<Lorem />
</React.Suspense>
);
@jamesseanwright
jamesseanwright / lazy-routes.tsx
Created July 17, 2019 16:40
Lazy route loading with React.lazy and Suspense
import * as React from 'react';
import Nav from './Nav.jsx';
import { Router } from './routing';
const routes = new Map<string, React.ComponentType>([
['/', () => <p>Pick an Ipsum!</p>],
['/lorem', React.lazy(() => import('./pages/Lorem'))],
['/bacon', React.lazy(() => import('./pages/Bacon'))],
['/hipster', React.lazy(() => import('./pages/Hipster'))],
['/office', React.lazy(() => import('./pages/Office'))],
@jamesseanwright
jamesseanwright / stackCreateElementRenderer.jsx
Last active October 11, 2019 08:56
Toy, stack-based HTML renderer with React.createElement-compliant element creator
'use strict';
const indentWidth = 2;
const selfClosingElements = [
'meta',
'link',
];
const indent = (string, depth) =>