Skip to content

Commit

Permalink
Type ReactRef
Browse files Browse the repository at this point in the history
Nothing out of the ordinary on this one.
  • Loading branch information
vjeux committed Aug 29, 2016
1 parent a3e576e commit 6a1166f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/isomorphic/classic/element/ReactElementType.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ReactElement = {
$$typeof: any,
type: any,
key: any,
ref: any,
ref: string | (elem: ?HTMLElement) => void,
props: any,
_owner: ReactInstance,

Expand Down
25 changes: 18 additions & 7 deletions src/renderers/shared/stack/reconciler/ReactRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactRef
* @flow
*/

'use strict';

var ReactOwner = require('ReactOwner');

import type { ReactInstance } from 'ReactInstanceType';
import type { ReactElement } from 'ReactElementType';

var ReactRef = {};

function attachRef(ref, component, owner) {
Expand All @@ -33,7 +37,10 @@ function detachRef(ref, component, owner) {
}
}

ReactRef.attachRefs = function(instance, element) {
ReactRef.attachRefs = function(
instance: ReactInstance,
element: ReactElement,
): void {
if (element === null || element === false) {
return;
}
Expand All @@ -43,7 +50,10 @@ ReactRef.attachRefs = function(instance, element) {
}
};

ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
ReactRef.shouldUpdateRefs = function(
prevElement: ?ReactElement,
nextElement: ?ReactElement,
): bool {
// If either the owner or a `ref` has changed, make sure the newest owner
// has stored a reference to `this`, and the previous owner (if different)
// has forgotten the reference to `this`. We use the element instead
Expand All @@ -56,20 +66,21 @@ ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
// is made. It probably belongs where the key checking and
// instantiateReactComponent is done.

var prevEmpty = prevElement === null || prevElement === false;
var nextEmpty = nextElement === null || nextElement === false;

return (
// This has a few false positives w/r/t empty components.
prevEmpty || nextEmpty ||
prevElement == null || prevElement === false || // prevEmpty
nextElement == null || nextElement === false || // nextEmpty
nextElement.ref !== prevElement.ref ||
// If owner changes but we have an unchanged function ref, don't update refs
(typeof nextElement.ref === 'string' &&
nextElement._owner !== prevElement._owner)
);
};

ReactRef.detachRefs = function(instance, element) {
ReactRef.detachRefs = function(
instance: ReactInstance,
element: ReactElement,
): void {
if (element === null || element === false) {
return;
}
Expand Down

0 comments on commit 6a1166f

Please sign in to comment.