Sha256: 27ca3dfbba00202ea3fdaf9b05ffc4ccb53d6dcbdb0fbf6240cbf079667c80f8

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

import { options } from 'preact';
import { assign } from './util';

let oldDiffHook = options._diff;
options._diff = vnode => {
	if (vnode.type && vnode.type._forwarded && vnode.ref) {
		vnode.props.ref = vnode.ref;
		vnode.ref = null;
	}
	if (oldDiffHook) oldDiffHook(vnode);
};

export const REACT_FORWARD_SYMBOL =
	(typeof Symbol != 'undefined' &&
		Symbol.for &&
		Symbol.for('react.forward_ref')) ||
	0xf47;

/**
 * Pass ref down to a child. This is mainly used in libraries with HOCs that
 * wrap components. Using `forwardRef` there is an easy way to get a reference
 * of the wrapped component instead of one of the wrapper itself.
 * @param {import('./index').ForwardFn} fn
 * @returns {import('./internal').FunctionComponent}
 */
export function forwardRef(fn) {
	function Forwarded(props) {
		let clone = assign({}, props);
		delete clone.ref;
		return fn(clone, props.ref || null);
	}

	// mobx-react checks for this being present
	Forwarded.$$typeof = REACT_FORWARD_SYMBOL;
	// mobx-react heavily relies on implementation details.
	// It expects an object here with a `render` property,
	// and prototype.render will fail. Without this
	// mobx-react throws.
	Forwarded.render = Forwarded;

	Forwarded.prototype.isReactComponent = Forwarded._forwarded = true;
	Forwarded.displayName = 'ForwardRef(' + (fn.displayName || fn.name) + ')';
	return Forwarded;
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
isomorfeus-preact-10.9.0 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.8.2 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.8.1 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.8.0 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.7.3 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.7.2 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.7.1 node_modules/preact/compat/src/forwardRef.js
isomorfeus-preact-10.7.0 node_modules/preact/compat/src/forwardRef.js