Skip to content

Instantly share code, notes, and snippets.

View icarus-sullivan's full-sized avatar

Chris Sullivan icarus-sullivan

  • Lewiston, ID
View GitHub Profile
@icarus-sullivan
icarus-sullivan / hooks_with_async_example.js
Last active July 4, 2019 19:14
Consuming HOC withAysnc
import React, { useState } from 'react';
import PageFetch from './PageFetch';
const Example = (props) => {
const [url, setUrl] = useState('https://www.npmjs.com/package/@sullivan/use-async');
return (
<div>
<div>Libraries</div>
<select value={url} onChange={(e) => setUrl(e.target.value)}>
@icarus-sullivan
icarus-sullivan / hooks_with_async_page.js
Last active July 4, 2019 19:13
Tie page fetch directly to useAsync
import React, { useState } from 'react';
import { withAsync } from '@sullivan/use-async';
const PageFetch = ({ loading, data, error, dispatch }) => (
<div>
<button enabled={!loading} onClick={dispatch}>Request Page</button>
<pre>{JSON.stringify({ loading, data, error })}</pre>
</div>
);
// ... useAysnc code above
export const withAsync = (fn, ...vargs]) => (Component) => ({ args, ...props }) => {
// If users override args in props, use those instead of vargs
const conditionalArgs = args || vargs;
// merge useAsync props with given props
const mergeProps = {
...props,
...useAsync(fn, ...conditionalArgs),
import React, { useState } from 'react';
import { useAsync } from '@sullivan/use-async';
const Example = (props) => {
const [url, setUrl] = useState('https://www.npmjs.com/package/@sullivan/use-async');
const { loading, data, error, dispatch } = useAsync(fetch, url);
return (
<div>
@icarus-sullivan
icarus-sullivan / hooks_use_async_example.js
Created July 4, 2019 18:37
Illustrates how to call useAsync
import React from 'react';
import { useAsync } from '@sullivan/use-async';
const Example = (props) => {
const { loading, data, error, dispatch } = useAsync(fetch, 'https://www.npmjs.com/package/@sullivan/use-async');
return (
<button onClick={dispatch}>Request Page</button>
);
};
@icarus-sullivan
icarus-sullivan / hooks_async_reducer.js
Created July 4, 2019 18:26
Keeping track of asyncronous calls with reducer
import React, { useReducer } from 'react';
const config = [
() => ({ loading: true }),
(data) => ({ loading: false, data }),
(error) => ({ loading: false, error }),
];
const asyncReducer = (_, { type, payload }) => config[type](payload);
@icarus-sullivan
icarus-sullivan / hooks_hoc_theme.js
Created July 4, 2019 17:10
Show the simplicity needed for a contextual HOC
import React, { useContext } from 'react';
import { usePersistState } from 'hooks/usePersistState';
const Context = React.createContext('default');
const Theme = ({ theme, children }) => {
const [_theme, setTheme] = usePersistState({ key: 'theme', defaultValue: theme });
return (
<Context.Provider value={{ theme: _theme, setTheme }}>
@icarus-sullivan
icarus-sullivan / hooks_external_hook.js
Last active July 5, 2019 20:37
Declaring an external hook, that we can pull into our components
import { useCallback, useState } from 'react';
export const usePersistState = ({ key, defaultValue }) => {
const [value, setValue] = useState(JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultValue)));
const setter = useCallback((value) => {
localStorage.setItem(key, JSON.stringify(value));
setValue(value);
});
custom:
domain:
fn::ternary:
- ${opt:stage}
- prod
- amazing.com
- fn::lower: ${opt:stage}.amazing.com
custom:
stagedDomain:
prod: amazing.com
domain: ${self:custom.stagedDomain.${opt:stage}, '${opt:stage}.amazing.com'}