by dustingetz · 11y ago · 0 comments
In idiomatic React code, most of the components you write will be stateless, simply composing other components. We’re introducing a new, simpler syntax for these components where you can take props as an argument and return the element you want to render:
// Using an ES2015 (ES6) arrow function:
var Aquarium = (props) => {
return <Tank>{getFish(props.species)}</Tank>;
};
// Or with destructuring and an implicit return, simply:
var Aquarium = ({species}) => (
<Tank>{getFish(species)}</Tank>
);
// Then use: <Aquarium species="rainbowfish" /> {Aquarium({ species: "rainbowfish" })}
I've come across many projects which defined lots of small react classes, usually with a one or two line render body, which would be better served as simple functions.The JSX gets converted to a simple function call.
The automatic semicolon insertion doesn't seem as bad for arrow functions, this works as expected:
() =>
"this string is returned"
I'd personally keep using the parenthesis for aesthetic reasons though.