prepack/test/react/functional-components/key-change-fragments.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

70 lines
1.8 KiB
JavaScript

var React = require('react');
// the JSX transform converts to React, so we need to add it back in
this['React'] = React;
// we can't use ES2015 classes in Prepack yet (they don't serialize)
// so we have to use ES5 instead
var Stateful = (function (superclass) {
function Stateful () {
superclass.apply(this, arguments);
this.state = { updated: false };
}
if ( superclass ) {
Stateful.__proto__ = superclass;
}
Stateful.prototype = Object.create( superclass && superclass.prototype );
Stateful.prototype.constructor = Stateful;
Stateful.prototype.componentWillReceiveProps = function componentWillReceiveProps() {
this.setState({ updated: true });
}
Stateful.prototype.render = function render () {
return (
<div>
{this.props.children}
(is update: {String(this.state.updated)})
</div>
);
};
return Stateful;
}(React.Component));
function App(props) {
if (props.switch) {
return (
<React.Fragment>
<Stateful key='hi' x={props.x}>Hi</Stateful>
</React.Fragment>
);
}
return [
<Stateful key='bye' x={props.x}>Bye</Stateful>
];
}
App.getTrials = function(renderer, Root) {
let results = [];
renderer.update(<Root switch={false} />);
results.push(['mount', renderer.toJSON()]);
renderer.update(<Root switch={false} />);
results.push(['update with same key', renderer.toJSON()]);
renderer.update(<Root switch={true} />);
results.push(['update with different key', renderer.toJSON()]);
renderer.update(<Root switch={true} />);
results.push(['update with same key (again)', renderer.toJSON()]);
renderer.update(<Root switch={false} />);
results.push(['update with different key (again)', renderer.toJSON()]);
return results;
};
if (this.__optimizeReactComponentTree) {
__optimizeReactComponentTree(App);
}
module.exports = App;