Title is misleading and can hinder inexperienced users from following this nice coding convention.
This totally depends on the situation. Just an example in React where most of the time you will write components, "export default class" is and always the most accurate and concise behavior because:
You never rename the class, and you should always have 1 class in a file. That's also how you do should consider doing in OOP by the way.
If there is a sub class, put it in a separate files
For backend, where you can export a lot of APIs from a file, it is better if you put exported components in an object, just like how you do it with module.exports.
The way we've been doing it where I work is you have /components/SomeComponent under there is a SomeComponent.js, which is written as a "plain component" without HOC wrapping it. So it'll be something like export default class. The index.js then pulls in that component and wraps it as necessary with HOC components and is an export default connect(SomeComponent).
That was the same setup I was using before I read about the convention I put above, which I really enjoyed and it reduces the amount of boilerplate and puts the connect call in the same file.
When you use selectors (located elsewhere), and action creators (also located elsewhere) with redux, it makes a lot of sense as your actual call to connect is normally only a few lines if that, and it always felt kind of silly to have a full file for that.
It makes more sense if you are doing ad-hoc "selectors" in your call to connect, or are combining several HOCs at once, as then it warrants the larger file, but that's more of a difference in overall architecture.
But in the end, as long as you have a plan and stick to it it's fine, the problems show up when you don't have any direction on how to structure things and every file ends up being different.
If you use redux, you should not export the class but “export default” the wrapper instead. There is no reason why sometimes you only want to export the class, but sometimes you export it wrapped in a Redux wrapper
But do you really want the class without Redux? In the end, redux is also a part of the logic a container has that needs to be tested.
I mean even in a test.js file, you will still want to test functionality that redux props bring? I don’t think it is a good idea to separate the logic of redux from the presentational component. But if you really need to test the presentational ones, you can directly import the class as we should isolate the container, which is wrapped by a connector, and the presentational component,
Yes, you really want the class without redux, you test redux separately. In fact, you test the action creators, the selectors, and the reducers, all separately.
You are talking about more of an integration test, or even higher level than that.
And why go through all that trouble of stubbing out what you need to just to get the component underneath, when you can just import it by name...
No extra downsides...
And, if you want to import the fully connected component for testing too, you can do that as well!
3
u/allicanseenow Aug 27 '18
Title is misleading and can hinder inexperienced users from following this nice coding convention.
This totally depends on the situation. Just an example in React where most of the time you will write components, "export default class" is and always the most accurate and concise behavior because:
You never rename the class, and you should always have 1 class in a file. That's also how you do should consider doing in OOP by the way.
If there is a sub class, put it in a separate files
For backend, where you can export a lot of APIs from a file, it is better if you put exported components in an object, just like how you do it with module.exports.