Skip to content

Instantly share code, notes, and snippets.

View erikras's full-sized avatar

Erik Rasmussen erikras

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / redux-dead-drop-example.js
Last active April 25, 2019 20:34
Redux Dead Drop Example
componentWillReceiveProps(nextProps) {
if(!this.props.deadDrop && nextProps.deadDrop) {
// dispatch an action to clear the value
this.props.clearDeadDrop()
// do what your case officer has instructed, Agent
this.performOperation(nextProps.deadDrop)
}
}
@erikras
erikras / InitializeFromStateForm.js
Created November 2, 2016 14:45
Example of initializing array fields from state
/**
* Answer to https://stackoverflow.com/questions/40349005/how-to-initialize-a-fieldarray-from-state-using-redux-form
*
* How to run:
*
* 1) Clone redux-form repo
* 2) Run the Initialize From State example (https://github.com/erikras/redux-form/tree/master/examples/initializeFromState)
* 3) Replace InitializeFromStateForm.js with this file
* 4) ...
* 5) Profit!