Add shallow-compare addon

Interestingly, I do not believe that the ComponentLifecycle interface is
propagated to components that inherit from React.Component.
This commit is contained in:
Phips Peter
2015-11-05 17:56:28 -08:00
parent 2ccdb76c47
commit cba13a3b25
2 changed files with 28 additions and 11 deletions
+11
View File
@@ -0,0 +1,11 @@
// Type definitions for React v0.14 (react-addons-css-transition-group)
// Project: http://facebook.github.io/react/
// Definitions by: Asana <https://asana.com>, AssureSign <http://www.assuresign.com>, Microsoft <https://microsoft.com>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="react.d.ts" />
declare module "react-addons-shallow-compare" {
function shallowCompare<P, S>(component: __React.Component<P, S>, nextProps: P, nextState: S): boolean;
export = shallowCompare;
}
+17 -11
View File
@@ -5,6 +5,7 @@
/// <reference path="react-addons-linked-state-mixin.d.ts" />
/// <reference path="react-addons-perf.d.ts" />
/// <reference path="react-addons-pure-render-mixin.d.ts" />
/// <reference path="react-addons-shallow-compare.d.ts" />
/// <reference path="react-addons-test-utils.d.ts" />
/// <reference path="react-addons-transition-group.d.ts" />
/// <reference path="react-addons-update.d.ts" />
@@ -16,6 +17,7 @@ import CSSTransitionGroup = require("react-addons-css-transition-group");
import LinkedStateMixin = require("react-addons-linked-state-mixin");
import Perf = require("react-addons-perf");
import PureRenderMixin = require("react-addons-pure-render-mixin");
import shallowCompare = require("react-addons-shallow-compare");
import TestUtils = require("react-addons-test-utils");
import TransitionGroup = require("react-addons-transition-group");
import update = require("react-addons-update");
@@ -88,34 +90,34 @@ var ClassicComponent: React.ClassicComponentClass<Props> =
class ModernComponent extends React.Component<Props, State>
implements React.ChildContextProvider<ChildContext> {
static propTypes: React.ValidationMap<Props> = {
foo: React.PropTypes.number
}
static contextTypes: React.ValidationMap<Context> = {
someValue: React.PropTypes.string
}
static childContextTypes: React.ValidationMap<ChildContext> = {
someOtherValue: React.PropTypes.string
}
static defaultProps: Props;
context: Context;
getChildContext() {
return {
someOtherValue: 'foo'
}
}
state = {
inputValue: this.context.someValue,
seconds: this.props.foo
}
reset() {
this.setState({
inputValue: this.context.someValue,
@@ -124,7 +126,7 @@ class ModernComponent extends React.Component<Props, State>
}
private _input: HTMLInputElement;
render() {
return React.DOM.div(null,
React.DOM.input({
@@ -132,6 +134,10 @@ class ModernComponent extends React.Component<Props, State>
value: this.state.inputValue
}));
}
shouldComponentUpdate(nextProps: Props, nextState: State, nextContext: any): boolean {
return shallowCompare(this, nextProps, nextState);
}
}
// React.createFactory
@@ -445,7 +451,7 @@ var renderer: React.ShallowRenderer =
renderer.render(React.createElement(Timer));
var output: React.ReactElement<React.Props<Timer>> =
renderer.getRenderOutput();
//
// TransitionGroup addon
// --------------------------------------------------------------------------
@@ -455,7 +461,7 @@ React.createFactory(TransitionGroup)({ component: "div" });
// update addon
// --------------------------------------------------------------------------
{
// These are copied from https://facebook.github.io/react/docs/update.html
// These are copied from https://facebook.github.io/react/docs/update.html
let initialArray = [1, 2, 3];
let newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4]