News for hackers. Stuff that matters.
Re:React: Mixins Considered Harmful by n0us on Wednesday July 13, 2016 @01:52PM (#12088184)
I personally moved all my projects away from mixins a while ago when I first heard they were deprecated. At first I was frustrated because of JS churn but this certainly was the right move.

For anyone who is apprehensive, the shift in thinking from using mixins to HOCs was not so difficult even if it's initially puzzling.

Quick edit: forgot to mention that this shift made my code way easier to understand in some places and thus the initial investment saves dev time in the long run.

Re:React: Mixins Considered Harmful by thatswrong0 on Wednesday July 13, 2016 @02:59PM (#12088720)
I'm a fan of HOCs as well. My only problem with them is that I'm also a fan of shallow testing, and HOCs don't play nicely with shallow testing. I'm waiting on something like this (https://github.com/airbnb/enzyme/issues/250) to get implemented
Re:React: Mixins Considered Harmful by mg74 on Wednesday July 13, 2016 @04:14PM (#12089295)
Export every component like this:

export class MyComponent extends React.Component { ... }

export default SomeHOC(MyComponent)

And in your test simply import the non-hoc version:

import { MyComponent } from './mycomponent'

Now the HOC won't get in the way of your testing.

Re:React: Mixins Considered Harmful by coderzach on Wednesday July 13, 2016 @03:09PM (#12088792)
Why not just mock the HOC to be the identity fn? `(Component) => Component`
Re:React: Mixins Considered Harmful by eiriklv on Wednesday July 13, 2016 @03:36PM (#12089013)
This is my preferred approach as well - and it (anecdotally) works great, since you can then just manually inject any props/spies as needed like any other "dumb" component.

Edit: But he might be pointing to the testing of the actual HOCs/component decorating functions.

Re:React: Mixins Considered Harmful by amelius on Wednesday July 13, 2016 @04:30PM (#12089432)
> I personally moved all my projects away from mixins a while ago when I first heard they were deprecated.

Mixins were actually one of the things that turned me away from React in the first place. Perhaps a good time to reconsider :)

(Are there any other known anti-patterns left in React?)

Re:React: Mixins Considered Harmful by danabramov on Wednesday July 13, 2016 @05:09PM (#12089685)
>Are there any other known anti-patterns left in React?

String refs and `findDOMNode()`. Both are anti-patterns but not deprecated yet. Both replaced by callback refs.

Re:React: Mixins Considered Harmful by adregan on Wednesday July 13, 2016 @07:15PM (#12090484)
Could you expand on string refs as an anti-pattern?
Re:React: Mixins Considered Harmful by Benjamin_Dobell on Wednesday July 13, 2016 @09:11PM (#12091043)
I wouldn't necessarily call them an anti-pattern, but they are at the very least not encouraged:

> Although string refs are not deprecated, they are considered legacy, and will likely be deprecated at some point in the future. Callback refs are preferred.[1]

I'm still using them in React Native, because I'm new and didn't realise there was an alternative until recently. To me there are three issues:

1. Magic. All you do is specify a string and somehow you end up with this.refs.<name>. Admittedly not that hard to follow, but magic is generally bad.

2. It's hard to keep track of what refs actually exist, which makes naming collisions hard to notice. There's just some arbitrary string in some JSX. Using explicit properties means they can be tracked much more easily e.g. Flow.

3. Again related to Flow, if you're declaring your properties as you should be, then Flow will tell you if swapping a referenced component type (checkbox for switch etc.) is going to cause problems elsewhere in code.

EDIT: I suppose you could already be declaring your refs with Flow. However, if you're doing that you're already 90% the way to using dedicated properties. In which case you might as well eliminate the magic and just use your own properties, rather than properties on refs. This makes searching for usage/references much more obvious; people/IDEs generally don't think of strings being a reference to a variable.

[1] https://facebook.github.io/react/docs/more-about-refs.html

Re:React: Mixins Considered Harmful by johncip on Wednesday July 13, 2016 @08:57PM (#12090987)
I'd like an answer to this as well. I've only ever needed string refs, and the callback refs are noisy. I can see where the React team may not want to support both, but are string refs actually bad in some way?
Re:React: Mixins Considered Harmful by danabramov on Thursday July 14, 2016 @08:45AM (#12093234)
String refs are bad in quite a few ways:

1. String refs are not composable. A wrapping component can’t “snoop” on a ref to a child if it already has an existing string ref. On the other hand, callback refs don’t have a single owner, so you can always compose them.

2. String refs don’t work with static analysis like Flow. Flow can’t guess the magic that framework does to make the string ref “appear” on `this.refs`, as well as its type (which could be different). Callback refs are friendlier to static analysis.

3. The owner for a string ref is determined by the currently executing component. This means that with a common “render callback” pattern (e.g. `<DataTable renderRow={this.renderRow} />`), the wrong component will own the ref (it will end up on `DataTable` instead of your component defining `renderRow`).

4. String refs force React to keep track of currently executing component. This is problematic because it makes `react` module stateful, and thus causes weird errors when `react` module is duplicated in the bundle.

This is why we want to move away from them in favor of callback refs that solve all those problems.

Re:React: Mixins Considered Harmful by developit on Monday July 18, 2016 @10:20AM (#12115162)
I think it's important people understand this. The last two points are precisely the reason String refs got moved from Preact's core into preact-compat.

Also, for the common-case usage of string refs, you can just use a helper to insert things into `this.refs`:

https://gist.github.com/developit/63e7a81a507c368f7fc0898076...

Re:React: Mixins Considered Harmful by johncip on Sunday July 17, 2016 @04:02AM (#12109422)
Thanks for the thorough answer, Dan.

(Had I not followed the prescription for the sake of being future-proof, I probably would have had a painful debugging session over #3. And possibly #1 too.)

Re:React: Mixins Considered Harmful by auvrw on Wednesday July 13, 2016 @02:22PM (#12088453)
from the article

> Let’s make it clear that mixins are not technically deprecated. If you use React.createClass(), you may keep using them. We only say that they didn’t work well for us, and so we won’t recommend using them in the future.

Re:React: Mixins Considered Harmful by rimunroe on Wednesday July 13, 2016 @02:25PM (#12088478)
I think around React 0.14, they announced that React's support for es6 class syntax wouldn't support mixins, and you'd only be able to get them by using the old .createClass method. I think they pointed out that they didn't see this as a big issue because mixins were a pattern they wanted to get away from anyway and saw better solutions to.