4 ms·
Why is mixing HTML into your code in PHP bad? Because it mixes presentation and logic. Why is mixing HTML into your code in React good? Because it isolates
by bryanlarsen 10y ago
Why is mixing HTML into your code in PHP bad? Because it mixes presentation and logic.
Why is mixing HTML into your code in React good? Because it isolates all the view code for a single component in a single file.
- tigershark 10y agoIn his example in the pre-preface there was quite a big amount of logic.. I don't really see the point in your post looking at that code.
- haukur 10y agoYou can have complex logic and functions in JSX but that doesn't mean that you have to, so the example is very contrived. That way of writing the code is completely discouraged in the documentation and I haven't run across it often or at all in the real world.
- autoreleasepool 10y agoThe example in the article was lifted from the front page of the official React website [0]. [0] https://facebook.github.io/react/ https://facebook.github.io/react/
- haukur 10y agoIt wasn't lifted from the docs. The tutorial is a tic-tac-toe game, while the example in the post is an AddTodo function [edit: there appears to be an add todo section, but the code is completely different]. Furthermore, all the examples in the tutorials show a pattern where you reference functions instead of writing them inside JSX.
- autoreleasepool 10y agoCheck again. On the homepage scroll down to: > An Application " Using props and state, we can put together a small Todo application. This example uses state to track the current list of items as well as the text that the user has entered. Although event handlers appear to be rendered inline, they will be collected and implemented using event delegation. "
- haukur 10y agoFair enough - it's a todo app. The code isn't structured in the same manner as the author's, however, so my point stands. It doesn't look like the code was lifted from the docs, as you said, but instead transformed to look like spaghetti.
- inopinatus 10y agoI thought that was irony, nuanced by a sly dig at Facebook. The gist being that React is what happens when a PHP shop creates a client-side framework, with all the architectural warts that this might imply, if you're on the PHP-lowers-the-bar-to-web-programming-too-far bandwagon. (FB engineers would probably tell you that their variant, Hack, is a far cry from PHP, and their coding standards are well above that of your bargain-basement wordpress drudge, but you get the idea)
- bryanlarsen 10y agoThe third paragraph was one that said that good PHP looks like React and bad React looks like PHP, but I deleted it because I thought it was stronger letting the reader make their own inference.
- throwaway2016a 10y agoTwo things there... 1. If PHP coders did component driven development it might not be so bad, the problem is most PHP apps mix code and HTML in a way that resembles speggeti so we (I've tought a lot of PHP courses) teach separation of concerns as to encourage maintainable code. 2. In React you're not really supposed to (as a matter of best practice) mix presentation and business logic. Typically most components are for presentation only with business logic done via event handlers and props. If you need state you should wrap your display component in a business logic component (typically called a container in the Redux world).