Skip to content

Instantly share code, notes, and snippets.

View erikras's full-sized avatar

Erik Rasmussen erikras

View GitHub Profile
const startTime = Date.now();
function generatePermutations(dictionary) {
const results = [];
const permute = (array, position) => {
if (position === array.length - 1) {
results.push(array);
} else {
for (let i = position; i < array.length; i++) {
const swap = array[position];
array.splice(position, 1, array[i]);
@erikras
erikras / MyContainer.js
Last active April 27, 2016 10:22
The HOC Drill Pattern [BEFORE]
import React, { Component } from 'react'
import MyWrappedComponent from './MyWrappedComponent'
export default class MyContainer extends Component {
constructor() {
// bind handlers
this.handleLoad = this.handleLoad.bind(this)
this.handleSave = this.handleSave.bind(this)
this.handleClear = this.handleClear.bind(this)
}
@erikras
erikras / MyContainer.js
Created April 27, 2016 10:40
The HOC Drill Pattern [AFTER]
import React, { Component } from 'react'
import MyWrappedComponent from './MyWrappedComponent'
export default class MyContainer extends Component {
constructor() {
// bind handlers
this.handleCallback = this.handleCallback.bind(this)
}
handleCallback(load, save, clear) {
@erikras
erikras / SearchBox.js
Last active September 23, 2020 03:03
A search box that replaces a query parameter in the url
import React, { Component, PropTypes } from 'react'
import { withRouter } from 'react-router'
import queryString from 'query-string'
@withRouter
export default class SearchBox extends Component {
static propTypes = {
router: PropTypes.object.isRequired
}
v6.2.2
3.9.5
redux-form@6.0.1 /Users/erik/oss/redux-form
├── array-findindex-polyfill@0.1.0
├─┬ babel-cli@6.14.0
│ ├─┬ babel-polyfill@6.13.0
│ │ └── regenerator-runtime@0.9.5
│ ├── babel-runtime@6.11.6
│ ├─┬ bin-version-check@2.1.0
│ │ ├─┬ bin-version@1.0.4
@erikras
erikras / example.js
Created August 29, 2016 16:13
Redux Form v5 -> v6 Migration Hint
/*
* As I have been migrating a project from redux-form v5 to v6, I have come across this useful pattern.
*
* I have many such "custom inputs and errors beside them" structures around my app.
*/
// v5 code
<div>
<MyCustomInputThing
{...myField}

Keybase proof

I hereby claim:

  • I am erikras on github.
  • I am erikras (https://keybase.io/erikras) on keybase.
  • I have a public key ASBKqAeWHhNv0eDi5m-_tjdje5Adf8HqJnK2FROuaIbdago

To claim this, I am signing this object:

@erikras
erikras / RichTextMarkdown.js
Created October 28, 2016 12:23
Using react-rte with redux-form
import React, { Component, PropTypes } from 'react'
class RichTextMarkdown extends Component {
static propTypes = {
input: PropTypes.shape({
onChange: PropTypes.func.isRequired,
value: PropTypes.string
}).isRequired
}
@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!
@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)
}
}