prepack/test/react/functional-components/class-root-with-refs.js
Dominic Gannaway afb0a9e9cb Change how to signal React components for optimization
Summary:
Release notes: none

Currently, you register a React component tree with `__registerReactComponent(component)`.

This PR changes this global to `__optimizeReactComponentTree(rootComponent, config)`. Notice it now supports two arguments `rootComponent` and `config` – although config is optional.

This global now also returns the original component passed in, so it can be added to existing codebase without having to break logic flow.

This config argument allows the user to define how that React component tree will be optimized. More work will be added to this in upcoming PRs, but for now this PR is just a quick rename plus small refactor of the Prepack global.

I've also had to rename the global in all tests. I've also added some doc as to how all this works: https://github.com/facebook/prepack/wiki/React-Compiler
Closes https://github.com/facebook/prepack/pull/1527

Differential Revision: D7149728

Pulled By: trueadm

fbshipit-source-id: 8d04d8dec8c0a03a6ccdb9587884bf6375010203
2018-03-04 02:24:49 -08:00

54 lines
1.3 KiB
JavaScript

var React = require('react');
// the JSX transform converts to React, so we need to add it back in
this['React'] = React;
function SubChild() {
return <span>Hello world</span>;
}
function Child() {
return <span><SubChild /></span>;
}
let instance = null;
// we can't use ES2015 classes in Prepack yet (they don't serialize)
// so we have to use ES5 instead
var App = (function (superclass) {
function App () {
superclass.apply(this, arguments);
this.divRefWorked = null;
instance = this;
}
if ( superclass ) {
App.__proto__ = superclass;
}
App.prototype = Object.create( superclass && superclass.prototype );
App.prototype.constructor = App;
App.prototype._renderChild = function () {
return <Child />;
};
App.prototype.divRefFunc = function divRefFunc (ref) {
this.divRefWorked = true;
};
App.prototype.render = function render () {
return <div ref={this.divRefFunc}>{this._renderChild()}</div>;
};
App.getTrials = function(renderer, Root) {
renderer.update(<Root />);
let results = [];
results.push(['render with class root', renderer.toJSON()]);
results.push(['get the ref', instance.divRefWorked]);
return results;
};
return App;
}(React.Component));
if (this.__optimizeReactComponentTree) {
__optimizeReactComponentTree(App);
}
module.exports = App;