Support Functional Components #57
Description
Moving from #34.
As of react@0.14
, plain functions can be used as components. In the future this will bring performance optimizations in React.
function Component(props) {
return <div/>;
}
Many people are already using these, there's even a Babel plugin (that I wrote) to transform pure component classes to pure component functions.
It would be great if babel-plugin-react-transform
supported these. However, there is a pretty big concern.
Since these are just plain functions, with JSX in them there is nothing that designates that it is a react component. i.e. in order to tell that a class is a react component, we can test that it extends React.Component
, or that it has a matching interface.
We can attempt to match them by treating "any function that has JSX in it will be treated as a component" i.e.:
function Component() {
return <div/>;
}
However, this would also match the inner function in here:
class Component extends React.Component {
render() {
const children = this.props.items.map(function(props) {
return <li>{props.name}</li>;
});
return <ul>{children}</ul>;
}
}
So we can tighten things and say "only top level components", but then we run into wanting to support factories:
function componentFactory() {
return function Component(props) {
return <div/>;
};
}
I could keep going for a while, you can put functions all over the place in JavaScript and no matter what we define as the rule here there will be exceptions. It's a matter of deciding what the heuristics should be that balances:
- Rarity - How often people will actually run into exceptions in real world code
- Obviousness - How easy it is to understand the reasoning behind an exception
It might be possible to get around the obviousness problem by throwing compile errors when running into ambiguous scenarios.