Skip to content

Instantly share code, notes, and snippets.

View jamesseanwright's full-sized avatar
🤔

James Wright jamesseanwright

🤔
View GitHub Profile
@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>
);
@jamesseanwright
jamesseanwright / example-link-usage.tsx
Created July 15, 2019 12:54
Example usage of Link component
@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],
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 / 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({
@jamesseanwright
jamesseanwright / ryor-action-creator.ts
Created December 22, 2018 17:52
Redux action creator example
export const addMessage = (message: string) => ({
type: ADD_MESSAGE,
payload: {
message,
},
});
@jamesseanwright
jamesseanwright / ryor-reducer.tsx
Created December 22, 2018 17:40
Example Reducer
const rootReducer: React.Reducer<State, Action> = (state, action) => {
if (isAddMessage(action)) {
const { message } = action.payload;
return {
...state,
isLoadingQuote: false,
hasQuoteError: false,
isFormValid: !!message,
messages: [
@jamesseanwright
jamesseanwright / ryor-augmented-dispatch.tsx
Last active July 12, 2020 07:01
Augmenting useReducer's dispatch
export type Thunk = (dispatch: React.Dispatch<Action>, state: State) => void;
export type AugmentedDispatch = React.Dispatch<Thunk | Action>;
const augmentDispatch = (dispatch: React.Dispatch<Action>, state: State) =>
(input: Thunk | Action) =>
input instanceof Function ? input(dispatch, state) : dispatch(input);
export const Provider: React.FC<ProviderProps> = ({
reducer,
children,
@jamesseanwright
jamesseanwright / redux-thunk.tsx
Created December 21, 2018 17:29
Redux Thunk Example
export const addRonSwansonQuote = () =>
(dispatch: React.Dispatch<Action>) => {
dispatch(setQuoteLoading());
return fetch('https://ron-swanson-quotes.herokuapp.com/v2/quotes')
.then(res => res.json())
.then(([quote]: string[]) =>
dispatch(addMessage(quote)),
)
.catch(() => dispatch(setQuoteError()));
@jamesseanwright
jamesseanwright / ryor-connect.tsx
Created December 21, 2018 17:00
Roll Your Own Redux Connect Impl
export const connect = <TStateProps, TDispatchProps, TOwnProps = {}>(
mapStateToProps?: (state: State, ownProps: TOwnProps) => TStateProps,
mapDispatchToProps?: (dispatch: AugmentedDispatch, ownProps: TOwnProps) => TDispatchProps,
) =>
(Component: React.ComponentType<TStateProps & TDispatchProps & TOwnProps>) => (
(props: TOwnProps) =>
<StateContext.Consumer>
{({ state, dispatch }) => (
<Component
{...props}