Skip to content

Instantly share code, notes, and snippets.

View jaredpalmer's full-sized avatar

Jared Palmer jaredpalmer

View GitHub Profile
@jaredpalmer
jaredpalmer / EmailInput.jsx
Created November 29, 2017 18:06
Formik async email signup input
import React from 'react';
import debounce from 'utils/debounce';
class EmailInput extends React.Component {
checkEmail = value => {
// only check if the field passes Yup email validation first
if (
!this.props.form.errors[this.props.name].includes(
'invalid' /* or whatever your error message is*/
)
@jaredpalmer
jaredpalmer / game.py
Created November 25, 2017 22:30
Python Classes
import math
def nCr(num, kVal):
"""Returns a nCr combination
Args:
num (int): The numerator.
kVal (str): The size of the set.
@jaredpalmer
jaredpalmer / emotion-jsxstyle.js
Last active November 8, 2017 14:43
jsxstyle everywhere!
import React from 'react';
import { css as EmotionCSS } from 'react-emotion';
import { jsxstyleFactory } from './jsxstyleFactory';
const cx = (css, styles, className) =>
EmotionCSS([{ ...css, ...styles }, className]);
const jsxstyle = jsxstyleFactory(cx);
export const Box = jsxstyle.Box;
@jaredpalmer
jaredpalmer / glamor-jsxstyle.js
Created October 19, 2017 14:33
glamor-jsxstyle dirty
import React from 'react';
import { css as Style } from 'glamor';
const View = ({
component = 'div',
props,
css,
className,
children,
...rest,
@jaredpalmer
jaredpalmer / prefetch.js
Created October 19, 2017 14:27 — forked from acdlite/prefetch.js
Prefetching in React
function prefetch(getKey, getValue, getInitialValue, propName) {
const inFlight = new Set();
const cache = new Map();
return ChildComponent => {
return class extends React.Component {
state = {value: getInitialValue(this.props)};
componentWillReceiveProps(nextProps) {
const key = getKey(nextProps);
if (cache.has(key)) {
// Use cached value
@jaredpalmer
jaredpalmer / Formik-Autosave.jsx
Last active December 29, 2022 01:22
Formik-Autosave
import React from 'react';
import PropTypes from 'prop-types'
import debounce from 'lodash.debounce' // or whatevs
import isEqual from 'lodash.isEqual'
class AutoSave extends React.Component {
static contextTypes = {
formik: PropTypes.object
}
import React from 'react'
import { Formik, Field, Form } from 'formik'
const MyInput = ({ field, form, ...rest }) =>
<div>
<input {...field} {...rest} />
{form.errors[field.name]
&& form.touched[field.name]
&& <div>{form.errors[field.name]}</div>}
</div>
import React from 'react'
import { Formik, Field, Form } from 'formik'
const App = () =>
<div>
<h1>My Login Form</h1>
<p>This can be anywhere in your application</p>
<Formik
initialValues={{ email: '', password: '', favorite: 'Kirk' }}
validate={values => { /* omitted for brevity */ }}
const Basic = () =>
<div>
<h1>My Form</h1>
<p>This can be anywhere in your application</p>
<Formik
initialValues={{ email: '', password: '' }}
validate={values => { /* omitted for brevity */ }}
onSubmit={values => { /* omitted for brevity */ }}
render={({ values, errors, touched, handleChange, handleSubmit, isSubmitting }) =>
<form onSubmit={handleSubmit}>
import * as React from 'react';
class ReducerComponent<Props, State> extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
}
reducer = (state: State, action: string) => {
// this should be
return state;