Skip to content

Instantly share code, notes, and snippets.

View alexkrolick's full-sized avatar
🎩
Open for consulting gigs!

Alex Krolick alexkrolick

🎩
Open for consulting gigs!
View GitHub Profile
@alexkrolick
alexkrolick / knex-mock.js
Last active February 23, 2018 06:00
Knex.js Mock
import _ from 'lodash'
// In-memory Knex mock 🤯
function DB () {
const db = function (table) {
if (!table) return db
db._table = table
if (!db._db[table]) {
db._db[table] = {
_latestId: 0,
@alexkrolick
alexkrolick / mini-reducer.jsx
Last active January 24, 2018 05:45
Redux-like reducer using just setState
const api = {
fetchUser(id) {
return { id, name: "username" };
}
};
class UserState extends React.Component {
state = {
user: null,
signinCount: 0,
// JSX
<div className="myClass">
<Foo selected />
<Bar />
{ [1,2,3].map(i => i**2) }
</div>
// Equivalent createElement
import React, { Component, createElement as h } from 'react'
const Foo = ({text, ...props}) => h("span", props, text)
const Bar = props => h("div", {},
h(Foo, { ...props, style: "color: blue" })
)
class Baz extends Component {
constructor(props) {
import React, { createElement as h } from 'react'
import { render } from 'react-dom'
const foo = h("i", {}, "Foo")
const bar = h("span", { style: "color: red;" }, "Bar", foo)
render(bar, document.body)
// JSX
const withFooProp = WrappedComponent => props => <WrappedComponent foo={2} {...props} />
// createElement
const withFooProp = WrappedComponent => props => h(WrappedComponent, { foo: 2, ...props })
const Bar // ... a component
export default withFooProp(Bar) // export an augmented version of Bar
// elsewhere...
class Container extends Component {
state = {
text: 'foo'
}
changeText = newText => this.setState({text: newText})
render() {
<div>
<Presenter1 text={this.state.text} changeText={this.changeText} />
<Presenter2 text={this.state.text} />
<div>
const InjectTimestamp = ({children, ...props}) => {
const time = new Date()
return children(time)
}
InjectTimestamp.propTypes = {
children: PropTypes.func,
}
const Foo = () => {
@alexkrolick
alexkrolick / script.jsx
Last active September 8, 2017 06:46
Renderless Components
/* Edit on Codepen: https://codepen.io/alexkrolick/pen/QMXMMv */
class StateContainer extends React.Component {
constructor(props) {
super(props);
}
get handlers() {
return {};
}
@alexkrolick
alexkrolick / retry.js
Created August 11, 2017 17:43
Async retry utility for Node
const backoffFns = {
constant: curDelay => curDelay,
geometric: curDelay => curDelay * 2,
}
const defaultOpts = {
maxTries: 2,
delay: 0,
backoff: 'constant',
logger: console.log,