Skip to content

Instantly share code, notes, and snippets.

View jrwebdev's full-sized avatar

James Ravenscroft jrwebdev

  • Auckland, New Zealand
View GitHub Profile
@jrwebdev
jrwebdev / loading-hoc.js
Created May 25, 2018 06:34
loading-hoc (js)
const withLoading = Component =>
class WithLoading extends React.Component {
render() {
const { loading, ...props } = this.props;
return loading ? <LoadingSpinner /> : <Component {...props} />;
}
};
@jrwebdev
jrwebdev / loading-hoc.tsx
Last active June 3, 2020 16:51
Loading HOC
interface WithLoadingProps {
loading: boolean;
}
const withLoading = <P extends object>(Component: React.ComponentType<P>) =>
class WithLoading extends React.Component<P & WithLoadingProps> {
render() {
const { loading, ...props } = this.props;
return loading ? <LoadingSpinner /> : <Component {...props as P} />;
}
@jrwebdev
jrwebdev / withBlueBackground.tsx
Last active February 9, 2018 19:55
withBlueBackground Subtract
import { Subtract } from 'utility-types';
const withBlueBackground = <P extends InjectedBlueBackgroundProps>(
UnwrappedComponent: React.ComponentType<P>
) =>
class WithBlueBackground extends React.Component<
Subtract<P, InjectedBlueBackgroundProps> & WithBlueBackgroundProps
> {
render() {
const { shade, ...props } = this.props;
@jrwebdev
jrwebdev / withBlueBackground.tsx
Last active February 9, 2018 19:56
withBlueBackground Omit
type Diff<T extends string, U extends string> = ({ [P in T]: P } &
{ [P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
const withBlueBackground = <P extends InjectedBlueBackgroundProps>(
UnwrappedComponent: React.ComponentType<P>
) =>
class WithBlueBackground extends React.Component<
Omit<P, keyof InjectedBlueBackgroundProps> & WithBlueBackgroundProps
> {
@jrwebdev
jrwebdev / Hello.tsx
Created February 9, 2018 09:45
Hello Pattern #3
import * as React from 'react';
import withBlueBackground, { InjectedBlueBackgroundProps } from './withBlueBackgroud';
interface HelloProps {
style?: React.CSSProperties;
name: string;
}
const Hello = ({ style, name }: HelloProps & InjectedBlueBackgroundProps) => (
@jrwebdev
jrwebdev / withBlueBackground.tsx
Created February 9, 2018 08:05
withBlueBackground Pattern #3
import * as React from 'react';
type ColorShade = 'light' | 'dark';
export interface InjectedBlueBackgroundProps {
backgroundColor: string;
}
interface WithBlueBackgroundProps {
shade?: ColorShade;
@jrwebdev
jrwebdev / withBlueBackground.tsx
Last active February 9, 2018 08:02
withBlueBackground Pattern #2
import * as React from 'react';
type ColorShade = 'light' | 'dark';
export interface InjectedBlueBackgroundProps {
style?: React.CSSProperties;
}
interface WithBlueBackgroundProps {
shade?: ColorShade;
@jrwebdev
jrwebdev / app.tsx
Last active February 9, 2018 06:38
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import withBlueBackground from './withBlueBackground';
import Hello from './Hello';
const BlueHello = withBlueBackground(Hello);
ReactDOM.render(
<BlueHello name="Bob" style={{ fontWeight: 'bold' }} />,
document.getElementById('app')
import * as React from 'react';
interface WithBlueBackgroundProps {
style?: React.CSSProperties;
}
const withBlueBackground = <P extends WithBlueBackgroundProps>(
UnwrappedComponent: React.ComponentType<P>
) =>
class WithBlueBackground extends React.Component<P> {
import * as React from 'react';
interface HelloProps {
style?: React.CSSProperties;
name: string;
}
const Hello = ({ style, name }: HelloProps) => (
<div style={style}>Hello, {name}!</div>
);