Skip to content

Instantly share code, notes, and snippets.

@jpgorman
jpgorman / SassMeister-input-HTML.html
Created October 14, 2015 08:06
Generated by SassMeister.com.
<div class="perspective">
<div class="bar cyan" role="progressbar" aria-valuenow="5" aria-valuemin="0" aria-valuemax="100">
<div class="bar-face roof percentage"></div>
<div class="bar-face back percentage"></div>
<div class="bar-face floor percentage"></div>
<div class="bar-face left"></div>
<div class="bar-face right"></div>
<div class="bar-face front percentage"></div>
</div>
</div>
@jpgorman
jpgorman / Loader.js
Created April 19, 2018 10:07
Example of loader component
class IsLoading extends React.Component {
constructor(props) {
super(props);
// initialise our state
this.state = { isLoading: false };
}
componentDidCatch(error, info) {
// if we have a promise then we can deal with it
if(error instanceof Promise) {
@jpgorman
jpgorman / AsyncComponent.js
Created April 19, 2018 10:15
Use the DataProvider
class AsyncComponent extends React.Component {
componentDidMount() {
// use the context
if(!this.context.state.foo)
dataFetcher();
}
...
}
AsyncComponent.contextTypes = {
@jpgorman
jpgorman / createWaiter.js
Last active April 20, 2018 15:49
Waiter example
// AsyncComponent.js
function mapStateToProps(state: IState) {
return {
data: waitForData(),
}
}
const AsynCompoment = connect(mapStateToProps)(({ data }) => <div>{console.log(data)}</div>)
// store.js
const waitForData = createWaiter(store, (state) => state.data)
@jpgorman
jpgorman / ReduxLoader.js
Last active April 19, 2018 10:38
Redux loader example
class IsLoading extends React.Component {
constructor(props) {
super(props)
this.state = { isLoading: false }
}
async componentDidCatch(err) {
if (err instanceof Promise) {
this.setState({ isLoading: true })
await err
this.setState({ isLoading: false })
@jpgorman
jpgorman / DataProvider.js
Created April 19, 2018 19:59
Example using context API
class DataProvider extends React.Component {
getChildContext() {
// we're omitting children from our props and passing all other props into context
const {children, ...rest} = this.props
return {
// we'll default to an empty object
state: rest || {}
};
}
@jpgorman
jpgorman / uploadFileSaga.js
Last active October 20, 2021 12:30
Example of using redux-saga eventChannel with axios/fetch etc
import {eventChannel, END} from 'redux-saga'
function createUploaderChannel(key, files){
return eventChannel(emit => {
const onProgress = ({total, loaded}) => {
const percentage = Math.round((loaded * 100) / total)
emit(percentage)
}
async function getComponent() {
const {default: module} = await import('../some-other-file')
const element = document.createElement('div')
element.innerHTML = module.render()
return element
}
// my-module.js
import * as React from 'react'
export default {
view: () => <div>My Modules View</div>
}
async function getComponent() {
const {default} = await import('./my-module')
return React.createElement(default.view)
})