News for hackers. Stuff that matters.
Re:Simple React Patterns by tw1010 on Friday November 10, 2017 @12:02AM (#15668059)
Part of me is sad that Javascript has now been made OOP, after a majority of efforts pushing the balance towards becoming more and more functional (e.g. PureScript) only a few years ago.
Re:Simple React Patterns by dunstad on Friday November 10, 2017 @12:24AM (#15668132)
React explicitly discourages using inheritance with components, instead promoting composing components together like functions. You're also encouraged to keep them stateless if possible, so they create the same output for the same input. I'll need some convincing that functional programming is on its way out in JavaScript.
Re:Simple React Patterns by personomas on Friday November 10, 2017 @12:31AM (#15668158)
Yeah, but why did they switch from `React.createClass({..})` to the class syntax? Classes are terrible in JS, they're not even native JS, it doesn't fit in JS. This change alone has brought more and more OO JS code.
Re:Simple React Patterns by acemarke on Friday November 10, 2017 @12:50AM (#15668226)
Prior to ES6, there were hundreds of incompatible class-like implementations in the JS ecosystem, and every major library had its own. That included React.

Now that ES6 classes are actually part of the language, there's no reason for the React team to continue maintaining their own class-like implementation. They can defer that to the language itself. This also enables better use of standardizing tooling around the JS language.

Re:Simple React Patterns by cname on Friday November 10, 2017 @12:53AM (#15668237)
I thought `createClass` does/did something essentially similar to what `class` does in ES6. Not having to use a framework's custom class-creation machinery doesn't seem like a big loss to me or like it is has anything to do with functional programming.
Re:Simple React Patterns by acemarke on Friday November 10, 2017 @01:06AM (#15668269)
Yep. It also had some differences in behavior. `createClass` auto-bound functions so that `this` always pointed to the component instance, and it supported mixins.

The React team now encourages functional forms of composition rather than use of mixins, so that's another reason why React.Component doesn't support them. The lack of automatic method binding has certainly been a major pain for people learning and using React, but it also means that there's no "magic" involved. The Stage 3 Class Properties syntax is the recommended way to ensure that methods are bound properly, and there's plenty of other possible solutions as well.

Re:Simple React Patterns by coldtea on Friday November 10, 2017 @07:09AM (#15669349)
Classes in JS6 are just a syntax sugar for the native JS prototype inheritance.

Nothing "terrible", absolutely native (both since the keyword is part of JS and since the implementation is based on prototypes that have been with JS since the beginning), and fits perfectly.

Re:Simple React Patterns by personomas on Friday November 10, 2017 @12:36AM (#15668179)
In React v0.13 https://reactjs.org/blog/2015/03/10/react-v0.13.html, they claimed the reason for the switch is because the `class` syntax is for "more flexibility." What can you do with the `class` syntax that you can't do with the functional syntax? Furthermore, you can generate React code much easier with the functional syntax than with the class syntax.

Also, the functional syntax has mixins and other ways to combine objects or share code, I think that would be harder to do with classes, which make them even less flexible.

Re:Simple React Patterns by always_good on Friday November 10, 2017 @01:10AM (#15668284)
Seems like a weird thing to miss for someone who ostensibly prefers functional programming.

For example, people here will talk about `function User() {}` like it's the pinnacle of amazing abstraction. Because it has the word "function" in it or something.

There's nothing functional about that to me. Mutating the prototype and dealing with the implicit `this` variable in your functions is about as far away from functional programming as you can get.

Meanwhile Javascript just gets more and more functional, from the mindshare of higher order functions/components gaining traction to the @decorator transformation to the Promise 'monad'. It just gets better and better.

I have a hard time taking anyone seriously who doesn't recognize the pain that `class` solved in the ecosystem whether you personally decide to use it or not. People have been writing OOP code in Javascript since day one, just with some of the worst idiosyncrasies of all OOP languages.

Re:Simple React Patterns by hungerstrike on Friday November 10, 2017 @02:34AM (#15668523)
People also write tons of bad functional code with JS. It gets worse and worse the more that people keep following this cargo cult of HOCs and functional programming. The obsession with using HOCs for passing a simple variable around is particularly absurd. Importing objects with methods (or even just a namespace with functions) is way, way cleaner than importing every function seperately.

Which stable and widely used GUI kit has ever been done well with functional programming? Until one exists, I can’t really take anybody who pushes functional programming for GUIs very seriously.

Functional programming just isn’t that good for complex domains. OOP is way better for GUIs.

Re:Simple React Patterns by girvo on Friday November 10, 2017 @05:11AM (#15668971)
ReasonML and ReasonReact agree with you: instead of HOCs, they prefer using standard functions, explicit “self”, and just passing things down through props. It’s quite a breath of fresh air really.
Re:Simple React Patterns by krysp on Friday November 10, 2017 @09:12AM (#15669951)
I suppose it isn't really a GUI but it does manage them.

http://xmonad.org/

Re:Simple React Patterns by acemarke on Friday November 10, 2017 @12:51AM (#15668229)
At the moment, React does not yet have support for stateful functional components. It's something they've said they would _like_ to research and implement down the road, but right now, component state and lifecycle methods requires class components.
Re:Simple React Patterns by bjeanes on Friday November 10, 2017 @01:12AM (#15668298)
Or the use of something like recompose[1]'s `lifecycle` function[2].

1: https://github.com/acdlite/recompose

2: https://github.com/acdlite/recompose/blob/8c4ac2e4/docs/API....

Re:Simple React Patterns by acemarke on Friday November 10, 2017 @01:17AM (#15668312)
Yeah, although that itself is implemented internally using an ES6 class:

https://github.com/acdlite/recompose/blob/8c4ac2e4a4cd8d60c8...

Re:Simple React Patterns by recursive on Friday November 10, 2017 @02:22AM (#15668495)
What do you mean native JS? It doesn't get much more native than "given keywords and syntax in the language and in the specification"
Re:Simple React Patterns by kowdermeister on Friday November 10, 2017 @02:53AM (#15668584)
Classes are perfectly fine in JS, they are just syntactic enhancements for managing prototypes more easily.
Re:Simple React Patterns by vinnymac on Friday November 10, 2017 @12:13PM (#15671298)
Not sure what you mean by 'just syntactic enhancements'. Check this out https://esdiscuss.org/topic/javascript-vision-thing#content-...
Re:Simple React Patterns by christophilus on Friday November 10, 2017 @09:27AM (#15670047)
ClojureScript's re-frame[0] is my favorite way to React, honestly. PureScript's halogen is also pretty nice. Re-frame has a built-in, functionally pure way to do ajax and side-effects, too, which comes in pretty handy.

[0] https://github.com/Day8/re-frame

Re:Simple React Patterns by escherize on Friday November 10, 2017 @09:32AM (#15670063)
I use reframes little brother, citrus with cljs and it has been awesome. The promise of data oriented frontend engineering really pays off.

https://github.com/roman01la/citrus

Re:Simple React Patterns by rlander on Friday November 10, 2017 @09:38AM (#15670103)
Awesome! I've used reagent/re-frame extensively for years, but I've never tried Rum because I couldn't find an obvious way to use it with re-frame.

What do you think of citrus so far? Is it production ready? Stable? Anything missing?

Re:Simple React Patterns by tmat on Friday November 10, 2017 @07:27AM (#15669415)
actually once redux is introduced to react it's very very very functional.. so hold the sadness
Re:Simple React Patterns by azangru on Friday November 10, 2017 @07:38AM (#15669446)
React is functional even without Redux :-)

Look at the Reason team, with their Reason-React implementation. Cheng Lou seems to actively dislike a single store that Redux is, and, if anything, co-locates stores to stateful components.

Re:Simple React Patterns by tmat on Saturday November 18, 2017 @09:00AM (#15728940)
That sounds more like flux, how do they avoid all the boilerplate that flux was? It was very very painful.