React-basic: Description of the conceptual model of React
github.com
github.com
Also of interest, the classic 'Rethinking best practices' presentation: https://www.youtube.com/watch?v=x7cQ3mrcKaY
React is a nice new point in the space, and is much more pragmatic than FRP or Rx.
Does anyone know of any immediate mode game engines using concepts from React?
The problem with gaming is that you usually don’t want to use the DOM, and React’s value add is mostly in its DOM handling.
this.element.innerHTML = "new value"
instead of this.setState({text: "new value"})
React's method of async state setting is really great when you have a bunch of components that know little to nothing about each other, because it'll automatically batch things up. But in some situations, and games are a perfect example, you need the value to change right now.Immediate mode simply means you specify what to redraw on every frame, there is no caching unless you specify it. And you basically redraw whenever some state changes (in a game this is going to be at frame rate).
In React, when some state changes, you respecify the DOM for components whose state has changed, but asynchronously the library determines how to make the DOM update more efficient on the next frame redraw.
Retained-mode caches by default (often in opaque ways), which was the whole point (only re-render parts of the scene that have changed). You can roll your own caching for immediate mode, usually via some kind of invalidation scheme (use image for a node if nothing changed for this component, otherwise call that node's re-render method). On the other side, projects like React takes the retained-mode DOM and make it look more like an immediate-mode abstraction without sacrificing so much performance.
So this is written by Sebastian who is part of the React core team. A lot of the earlier stuff might seem obvious to most but I think it gets increasingly non obvious as you go down the list. I would direct your attention to the bit at the bottom on Algebraic Effects (https://github.com/reactjs/react-basic#algebraic-effects) - that came up on twitter today in relation to what the mental model behind React Suspense is (even though technically JS doesnt have Algebraic Effects itself, thats kind of what they are trying to do with async rendering.) I'll let gaearon and co explain better if they see this, I don't want to butcher it.
React isn't really inventing anything fundamentally new in terms of computer science but I think they are introducing a lot of people to good concepts that already existed.
Douglas Crockford was writing about it for JS [0] back in 2008 which is still a long way behind compilers and, say, Haskell but it has been used in JavaScript at least a decade.
[0]: https://stackoverflow.com/questions/3798858/explanation-on-j...
function FancyUserBox(user) {
return {
borderStyle: '1px solid blue'
...
Perhaps it's just me being pedantic and these are just examples. But when I see React examples that set style properties directly it makes me cringe, like it's ok to go back to a time before CSS just because everything's in JSX.It's based off the premise that content and styling are independent. It works very well when that is true. Often they are not.
If I'm designing a "date-picker" component, it will be completely broken without the corresponding styling. There can be no separation of content and style, the content does not make sense without the styling to produce the actual final output.
Such components require style definitions to function.
You're the first person I've heard wanting to scrap CSS and go back to style tags. Sure people prefer LESS/SASS but having styling in a different file and not mixed into JSX code makes style changes simpler for the way I work.
There are two ways to look at your code: https://i2.wp.com/pbs.twimg.com/media/DCXJ_tjXoAAoBbu.jpg?w=...
React mostly makes you think in terms of components.
Also: React doesn't output HTML.
React renders to the DOM which outputs HTML. It's still HTML that can be styled with CSS independently of React.
What do you think the code you complained about does?
> But scrapping CSS for style tags is a bad idea.
It's not bad in this particular case because these were simple examples to illustrate an entirely different point.
> React renders to the DOM which outputs HTML. It's still HTML
Erm. No. DOM never outputs any HTML. There's no HTML generated when you use React (or any other DOM-manipulation library or vanilla Javascript).
It's the other way around:
- HTML gets parsed
- A DOM (Document Object Model) is created to provide an in-memory, well, object model of the HTML document.
- In order to render anything on screen the browser works with the DOM only
- When something manipulates the DOM directly, the browser reacts to those changes and re-renders the view accordingly
- There's no HTML generated
- If someone does change the HTML, it's re-parsed, go to step 1.
Yes, you can style DOM nodes independently of React because that's how CSS works. However, there is a reason why people hate CSS with a passion: it doesn't scale, it's cumbersome to develop components with it etc. etc. etc. And that's why people ended up inventing ways to include/generate CSS via JS alongside component code.
> these were simple examples to illustrate an entirely different point.
I did accept this in my original post, it could be just because they were examples. But it's trivial to use classes instead.
I've never come across many people that hate CSS with a passion. It's been used to create billions of pages so I'm sure that some people will hate it but it was certainly a step up from individual style tags.
The React docs agree that it's more efficient to use CSS instead of inline styles [0]. And beyond that when talking about CSS-in-JS:
> if in doubt, a good starting point is to define your styles in a separate *.css file as usual and refer to them using className.
There are problems with CSS, but it also solved a lot of problems that came before that seem to have been forgotten about.
Imagine you had a complex database of authors and books, and every time you read from it you want to receive the name of the user or the book in title case. You see that this behavior can be abstracted over, so you say "any time I pull a record from the database, no matter what table it comes from, capitalize its title". This is very convenient.
Well, no one would ever do this in a complex application, because you don't know what sort of implications it might have for future tables that are added - or what if you decide that short words in the title should be lowercase? Now you are making exceptions instead of declarations.
This is effectively what CSS does. it unnecessarily couple's visual state to other visual state, unless it is namespaced uniquely to a specific component.
> However, there is a reason why people hate CSS with a passion
So it seems some people do. Going through some of the talks about CSS in JS it seems Facebook and Airbnb are making good cases for the problems they're hitting with CSS especially around asynchronous loading of CSS files.
But having spent lots of time seeing what a mess developers come up with using style tags on individual elements before switching to using CSS I'm interested to see what happens when there's tons of React components with hard coded styles. It seems like a solution that's ripe for creating more problems than it solves.
The problem is that css scales poorly for large projects, especially when you build many components and mix them together. It is almost impossible to know which css rules are in effect for a specific part when you don't know where that part will end up and it is also hard to create css rules that continue to work since more parts and html may be added later which changes the css rule matching.
If you're worried about class overlap, you can define unique/hash-based classnames on build with css-loader.
If you're still worried about aggressive stylesheets destroying your components, you can wrap them in however many classes you see fit (some people might use `.n.e.a.t.l.i.b .cool-class` instead of polluting component code with CSS).
As far as CSS scaling poorly, that's really up to you. I find that it's an afterthought to most people, and full of weird hacks and `!important`s a few months into a project. That's fine if that's how you want to do it, but it's not an inevitability.
(I say this as someone working on a component library that's used on over half a million sites)
I find this idea a little controversial - it is certainly true for websites, but I don't think it's necessarily the case for single page applications that have a purpose beyond showing data.
I think in those cases the 'natural hierarchy' approach tends to break and the idea of passing arguments in a hierarchical manner can become cumbersome. At least that is my experience so far,
https://github.com/glassechidna/practical-react-ssr
I don't think there's anything particularly revolutionary in there, it's just a reasonable starting point to hit the ground running.
If you've looked at the template and there was something you thought to be sub-standard, then I'd love to know what people would do differently.
If the down votes were because I made another React template, then, ah, that's somewhat understandable. Although, as I've stated, it was created for our own internal use, and was simply open sourced because we love open source and giving back to the community. Additionally, at the time of creation there absolutely weren't any full-scale open source projects (templates or otherwise) available using Babel 7 and Webpack 4. Actually, Babel 7 is still not officially released yet, so there really aren't many even now.
If the down-votes were because you think the comment is unrelated to the source article, well... I don't know precisely how to respond to that critique. However, the article (and this discussion) is regarding the underlying principles of React and the linked project is a practical implementation of React (and the underlying principles). I've specifically added a new root level comment rather than piggy-backing off other unrelated comments, which is unfortunately commonly seen on HN.
Is the sharing of related (non-competing) open source projects seen as poor etiquette?
As it is, it just feels like "This post is about React, so here's my project that is also about React." So I can see why it's getting downvoted.
I guess that's fair, however my intention is still very much to be constructive. Instead of critiquing the article, I'm attempting to provide value to the target audience of this article (likely those new to React), in the form of a code-base to get those interested up and running with React.
> As it is, it just feels like "This post is about React, so here's my project that is also about React." So I can see why it's getting downvoted.
I feel as though saying "Hey, seems as you like X, you may well be interested in some related Y" is actually pretty normal human conversation.
I would have thought that it's only spammy if there's some hidden agenda. There's no "premium version" or any such nonsense, the only thing I gain by sharing this is the satisfaction of knowing others may benefit from something I built - which honestly is pretty great! However, I don't think a supportive and constructive community should take offense to that.
Additionally, it's pretty normal to see links to related projects on HN. I guess the down-fall here is that templates, whilst incredibly useful to some, are dead boring to those who do not require them. If the linked project were more interesting I'm sure the response would be more favourable.
I look at react, and I see... shit. As an aside, I'm taking a deep dive reading u on the nuances of JS, so I'm reading along here and I get whats going on at a technical level with the JS; but the actual react code... does not jive with me.
Remember when we though SOAP was cool? React kinda feels like that to me. I don't think it really represents a way forward in programming.
But you know - I respect that there might be people who totally jive with react, who's brains work differently from mine and see nothing but awesome.
This also makes me so, so thankful that I am a Rails developer. React is surely good for certain things, but developer happiness and productivity i think are not among them.
And, as a Ruby developer going to React, I overwhelmingly disagree with regards to both developer happiness and productivity; I write better, more testable code, faster, when building out a React frontend than I have in any other system I've ever used, bar none.
Anyway, your comments don't really add anything unless you share what you think is an accurate representation of UI - otherwise, there's nothing we can learn from it, other than that you disagree with the design of React.
EDIT: maybe it's 'just picking a fight' for the sake of it? https://news.ycombinator.com/item?id=16430826
I feel one way, others feel a different way... who's right? who's wrong? all a matter of what matters to you. For what matters to me, react is totally wrong. I am still trying to "see the light" but I have yet to.
It just looks like cargo culting and/or flame baiting.
(FWIW, I find React, or the entire 'front-end' ecosystem to still be too complicated and unstable, and usually not worth the benefits SPA's offer. So after about a decade of front-end work I've become quite conservative and have focused my efforts on becoming better at back-end stuff.)
Anyways, I have always had immense difficulty accepting someone sharing an opinion without them explaining why, and it's often led me down paths that in hindsight didn't seem very productive or for that matter justified.
So I think I agree with what you've been saying so far and I'll take this whole thing as a personal lesson to learn to deal with that 'quirk' of mine. Because it probably does upset my calm more than I'm aware of and than it's worth. Thanks for the reminder.
I do. Because I just might learn something!
But I'll be frank: after your posts in this thread, and the thread linked here, I don't think that wall of text exists, I have some significant skepticism with regards to your ability to generate a meaningful one, and I think you are appealing to it as a rhetorical trick.
So, for anyone reading this and still being confused by react, please step back and think about what you need to know and understand right now which may be much less than everything there is to know about react and if you find some part that you don't like you may be able to avoid it.
I'm amazed at the amount of complexity that people seem try and heap on top of React. It's like they have seen the core simplicity and are desperate to complexity it to make it feel like they are dealing with something weighty and significant.
Simple things can be brilliant, React is a simple thing. A simple idea with a simple to use implementation. It's clever. It's good. There is no need to make it complex.
How can you do that otherwise?
PS: i agree that WADL or Swagger can do something similar, more elegantly than SOAP. But can we agree that there is a real need to enforce the coherence between the client and the server ?
I assume you haven't done much front-end engineering then. Because for all of the many legitimate gripes with React, developer happiness and productivity is one of its strengths. Going from something like manual Backbone views to React components was a breath of fresh air when React was released (there are now other frameworks that'll do the same, but still).
Rails is not a front-end framework, so basically you're saying "I am thankful I am a backend developer, not a frontend one".