Skip to content

Instantly share code, notes, and snippets.

@gilbox
gilbox / Functional React: The Renderer Component.js
Last active August 29, 2015 14:23
Functional React: The Renderer Component
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const rendererMixin = {
getInitialState() {
return {data:this.props.data}
},
edit (transform) {
this.setState({data: transform(this.state.data)})
}
@gilbox
gilbox / Functional React: subedit.js
Created June 13, 2015 08:57
Functional React: subedit
const subedit = (edit, ...path) => transform =>
edit(state => state.updateIn(path, transform));
@gilbox
gilbox / Functional React: Rudimentary State Changes.js
Last active August 29, 2015 14:23
Functional React: Rudimentary State Change
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const Hello = component(function Hello ({name}, {edit}) {
return <div>
Hello, {name}!
<button onClick={event => edit(data => data.updateIn(['name'], 'Maude Lebowski') }>
Change My Name
</button>
</div>
@gilbox
gilbox / Functional React: Encapsulated State Changes.js
Last active August 29, 2015 14:23
Functional React: Encapsulated State Changes
const {fromJS} = require('immutable');
const data = fromJS({ name: 'gilbox' });
const Hello = component(function Hello ({name}, {edit}) {
return <div>
Hello, {name}!
<button onClick={event => edit(name => 'Maude Lebowski') }>
Change My Name
</button>
</div>
@gilbox
gilbox / Functional React: Component Function with ComponentRenderMixin.js
Created June 13, 2015 09:26
Functional React: Component Function with ComponentRenderMixin
// @param additionalMixins? {Array|Object}
// @param renderFn {Function}
// @returns Component
function component(additionalMixins, renderFn) {
renderFn = renderFn || additionalMixins;
additionalMixins = (additionalMixins instanceof Function) ? [] : [].concat(additionalMixins);
const mixins = [ComponentRenderMixin].concat(additionalMixins);
const render = function() {
@gilbox
gilbox / Functional React: component with ComponentRenderMixin staticFunctionsMixin.js
Last active August 29, 2015 14:23
Functional React: component Function with ComponentRenderMixin and staticFunctionsMixin
// ComponentRenderMixin is a slightly modified version of PureRenderMixin
const ComponentRenderMixin = require('component-render-mixin');
function delegate(delegee) {
var delegate = function() {
return delegate.delegee.apply(this, arguments);
}
delegate.delegee = delegee;
delegate.isDelegate = true;
return delegate;
@gilbox
gilbox / Functional React: Phone Input Demo.js
Created June 14, 2015 16:33
Functional React: Phone Input Demo
const {fromJS} = Immutable;
const subedit = (edit, ...path) => transform =>
edit(state => state.updateIn(path, transform));
const data = fromJS({
phone: '6665552222'
});
const parsePhone = v => v.replace(/\D/g, '').substr(0,10);
@gilbox
gilbox / classy-react-creating-a-component.js
Created June 30, 2015 04:48
Classy Functional React: Creating a Component
@elegant
class Hello extends Component {
render() {
const {name} = this.props;
return <div>Hello, {name}!</div>
}
}
@gilbox
gilbox / functional-react-component-function.js
Created June 30, 2015 04:56
Functional React: component function
// @param additionalMixins? {Array|Object}
// @param renderFn {Function}
// @returns Component
function component(additionalMixins, renderFn) {
renderFn = renderFn || additionalMixins;
additionalMixins = (additionalMixins instanceof Function) ? [] : [].concat(additionalMixins);
const mixins = [ComponentRenderMixin,
staticFunctionsMixin]
.concat(additionalMixins);
@gilbox
gilbox / classy-react-elegant-decorator.js
Last active August 29, 2015 14:23
Classy React: elegant decorator
import {shouldComponentUpdate} from '../component-render-mixin';
import {componentWillMount, componentWillReceiveProps} from '../statics-mixin';
function elegant(DecoratedComponent) {
return class ElegantDecorator extends React.Component {
static displayName = `Elegant(${getDisplayName(DecoratedComponent)})`;
static DecoratedComponent = DecoratedComponent;
shouldComponentUpdate(nextProps, nextState) {
return shouldComponentUpdate.call(this, nextProps, nextState);