UI components: Angular vs React
Please don't reinvent JavaScript in your web framework
I’ve been building web UIs in Angular and React for the last few years, and I’ve started to greatly prefer React. Extracting and using UI components is just easier and, for lack of a better word, more JavaScripty.
Extracting components in React
Say I notice that I’m creating multiple <span>
elements with the same class and icon:
function Main() {
return (
<div>
<span class="alert-tag"><i class="fa alert"></i>foo</span>
<span class="alert-tag"><i class="fa alert"></i>bar</span>
<span class="alert-tag"><i class="fa alert"></i>baz</span>
</div>
);
}
It’s trivial to refactor this repeated element into its own <Alert>
component function within the same file:
function Main() {
return (
<div>
<Alert>foo</Alert>
<Alert>bar</Alert>
<Alert>baz</Alert>
</div>
);
}
function Alert({ children }) {
return (<span class="alert-tag"><i class="fa alert"></i>{children}</span>);
}
Super easy, uses JavaScript’s native language constructs, and I was able to do it all within the same file (but I can easily move Alert()
elsewhere for wider re-use if needed). It’s just like extracting a function in a “regular” programming language, it’s something you do without even thinking about it.