Skip to content

Instantly share code, notes, and snippets.

View erikras's full-sized avatar

Erik Rasmussen erikras

View GitHub Profile
@erikras
erikras / FieldArraysForm.js
Created November 14, 2016 22:19
Example showing how to use a formValueSelector on an individual array item
/**
The following can replace the file in the Field Arrays example
(https://github.com/erikras/redux-form/tree/master/examples/fieldArrays) to demonstrate this functionality.
**/
import React from 'react'
import { connect } from 'react-redux'
import { Field, FieldArray, reduxForm, formValueSelector } from 'redux-form'
import validate from './validate'
@erikras
erikras / problem.js
Last active August 11, 2017 09:29
React Performance Anti-Pattern: Creating Functions in render() - problem.js
import MyCustomButton from './ui/MyCustomButton'
class MyClicker extends Component {
constructor() {
super()
this.state = { clicked: false }
}
render() {
return <div>
@erikras
erikras / attempt.js
Created August 11, 2017 09:28
React Performance Anti-Pattern: Creating Functions in render() - attempt.js
import MyCustomButton from './ui/MyCustomButton'
class MyClicker extends Component {
constructor() {
super()
this.state = { clicked: false }
}
handleClick() {
this.setState({ clicked: true })
@erikras
erikras / solution1.js
Created August 11, 2017 09:31
React Performance Anti-Pattern: Creating Functions in render() - solution1.js
import MyCustomButton from './ui/MyCustomButton'
class MyClicker extends Component {
constructor() {
super()
this.state = { clicked: false }
this.handleClick = this.handleClick.bind(this) // Problem solved!
}
handleClick() {
@erikras
erikras / solution2.js
Created August 11, 2017 09:32
React Performance Anti-Pattern: Creating Functions in render() - solution2.js
import MyCustomButton from './ui/MyCustomButton'
class MyClicker extends Component {
constructor() {
super()
this.state = { clicked: false }
}
handleClick = () => {
// "this" is the right instance of this component
@erikras
erikras / mutators.js
Last active December 3, 2017 16:16
🏁 Final Form Mutator Example
/** Clears a form value */
const clear = ([name], state, { changeValue }) => {
changeValue(state, name, () => undefined)
}
/** Converts a form value to uppercase **/
const upper = ([name], state, { changeValue }) => {
changeValue(state, name, value => value && value.toUpperCase())
}
@erikras
erikras / arrays.js
Last active December 3, 2017 16:21
🏁 Final Form Arrays
import { createForm } from 'final-form'
import arrayMutators from 'final-form-arrays'
// Create Form
const form = createForm({
mutators: {
// potentially other mutators here
...arrayMutators
},
onSubmit
@erikras
erikras / final-form-calculate-usage.js
Created December 11, 2017 10:52
Usage example of final-form-calculate
import { createForm } from 'final-form'
import createDecorator from 'final-form-calculate'
// Create Form
const form = createForm({ onSubmit })
// Create Decorator
const decorator = createDecorator(
// Calculations:
{
@erikras
erikras / final-form-set-field-data-usage.js
Created December 11, 2017 11:00
Usage example for final-form-set-field-data.
import { createForm } from 'final-form'
import setFieldData from 'final-form-set-field-data'
// Create Form
const form = createForm({
mutators: { setFieldData },
onSubmit
})
form.mutators.setFieldData('firstName', { awesome: true })
@erikras
erikras / warning-engine.js
Created December 11, 2017 11:17
A warning engine implemented in 🏁 React Final Form
const WarningEngine = ({ mutators: { setFieldData } }) => (
// FormSpy lets you listen to any part of the form state you want.
// If you provide an onChange prop, FormSpy will not render to the DOM.
<FormSpy
subscription={{ values: true }}
onChange={({ values }) => {
setFieldData('firstName', {
warning: values.firstName ? undefined : 'Recommended'
})
setFieldData('lastName', {