Skip to content

Instantly share code, notes, and snippets.

@dounan
dounan / react-inline-fn-benchmark-results.txt
Last active December 5, 2017 18:32
React inline fn benchmark results
Code:
https://github.com/dounan/react-inline-fn-benchmark/tree/master
Try it out yourself here:
https://dounan.github.io/react-inline-fn-benchmark/
The numbers below show the time (ms) it takes to render 1000 buttons 1000 times.
- For the "Class property fn" tests, each button has an `onClick` callback that is a function on the button component
instance.
- For the "Inline fn" tests, each button has an `onClick` callback that is an inline arrow function.
@dounan
dounan / callback-within-render-callback.jsx
Last active November 1, 2017 23:10
Callback within a render callback example
/*
https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce
The render callbak examples in the blog post only pass data to the
render callback, and the render callback simply renders the data.
What if wanted to pass another callback as an argument to the render
callback? Does this pattern feel ok?
@dounan
dounan / inline-fn-react-example.jsx
Created October 31, 2017 21:50
Inline function in react example
class MyComponent extends React.Component {
render() {
const msg = "Hello " + this.props.user.name.first;
return <PureChild onClick={() => this.props.onAlert(msg)} />;
}
}
@dounan
dounan / transform-bind-reflective-bind.jsx
Last active October 31, 2017 02:37
Example of transforming a bind call with reflective-bind
// ======================================================
// Original code
function MyComponent(props) {
return <PureChild onClick={alert.bind(null, "Hello world")} />
}
// ======================================================
// After reflective-bind/babel transforms the code
@dounan
dounan / reflective-bind-implementation.js
Created October 18, 2017 23:24
reflective-bind-implementation
function reflectiveBind(f, ctx, ...args) {
const result = f.bind(ctx, ...args);
Object.defineProperty(result, "__func", {value: f});
Object.defineProperty(result, "__ctx", {value: ctx});
Object.defineProperty(result, "__args", {value: args});
return result;
}
function reflectiveEqual(f, g) {
return (
@dounan
dounan / reflective-bind-pure-component-monkeypatch.js
Last active October 24, 2017 20:48
reflective-bind PureComponent monkeypatch
import {shouldComponentUpdate} from "reflective-bind";
React.PureComponent.prototype.shouldComponentUpdate = function(
nextProps,
nextState
) {
return shouldComponentUpdate(this, nextProps, nextState);
};
@dounan
dounan / reflective-bind-scu.jsx
Created October 18, 2017 22:54
reflective-bind shouldComponentUpdate example
import {shouldComponentUpdate} from "reflective-bind";
class MyComponent extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return shouldComponentUpdate(this, nextProps, nextState);
}
...
}
@dounan
dounan / memoized-bind-example.js
Last active October 18, 2017 11:36
Memoized bind implementation and example
function memoizedBind(fn, ...args) {
// Memoize the bind call (with cache size 1).
if (!areArgumentsShallowlyEqual(fn.__lastArgs, args)) {
fn.__lastResult = fn.bind.apply(fn, args);
fn.__lastArgs = args;
}
return fn.__lastResult;
}
// ======================================================
@dounan
dounan / hoist-arrow-fn-reflective-bind.jsx
Last active October 20, 2017 20:33
Example of hoisting an arrow function with reflective-bind
// ======================================================
// Original code
function MyComponent(props) {
const msg = "Hello " + props.user.name.first;
return <PureChild onClick={() => alert(msg)} />
}
// ======================================================
// After reflective-bind/babel transforms the code
@dounan
dounan / reflective-bind-simple.jsx
Last active October 18, 2017 06:23
Simple reflective-bind example
function MyComponent(props) {
const msg = "Hello " + props.user.name.first;
return <PureChild onClick={() => alert(msg)} />
}