React in patterns
github.com
github.com
export default wire(Title, ['title'], function resolve(title) {
return { title };
});
Now I have to register "title" somewhere. Where is it registered? Did I remember to register it? I no longer know what's going on just by looking at the file in front of me, and my linter is silent. Then I have to annotate the function and declare it in the argument list. Items will be added and removed from the two lists as the app evolves, introducing more mental overhead and increased surface area for bugs, since once again my linter will have nothing to say on the topic.Surely I'm in the minority given how popular Angular was, but I felt "DI" was the absolute worst thing about Angular's approach—basically a DSL layered over JS requiring yet more specialized tooling. The ability to look at just one file and understand it goes hand-in-hand with static analyzability, and I'd hate for the React community to abandon that and go down this path.
[edit] In the spirit of not dwelling on the negative, I should also say I did find most of the article to be a good summary of design patterns. Nice job!
The DI in Angular made a little bit of sense because JS modules weren't very widespread back then. But now, with ES6 imports (and/or 'require') there's no need. We have real modules now, and they can be dynamically injected for testing purposes with something like inject-loader[0].
I think Angular and/or DI's popularity has something to do with Java devs learning frontend development, and feeling more comfortable with more structure.
For example, oftentimes one has a class that may have an instance floating around. One does not want to instantiate it unnecessarily if it already exists oftentimes, and so the injector will handle that automatically if necessary by instantiating an instance if it doesn't exist already, allowing for efficient memory management of services. It also has a major benefit for facilitating easier patterns for testing & readability.
Angular 1's DI has a poor API signature, with the naive allowance of it through a hacky regex, and crappy duplication of string references for the regular usage with minification taken into account - Angular 2's is built much more solidly and without such hacks.
It should also be noted that using cjs for dynamic injection is a bit of a hack as well, since it is a non-standard syntax. Angular 2's DI is much more pure, as it could be split off into a standalone library with some build configuration with zero external dependencies other than perhaps an ES6 shim like core.js.
foo(X dep) {} vs foo() {X dep = new X()}
It's overused imo but it's fine. The problem comes with DI frameworks like Spring or Angular DI when you start letting the framework tie your code together. This couples you tightly with the framework and can lead to odd situations where the thing you're getting isn't what you're expecting but that's not obvious because the object passing is happening behind a curtain.
It is just a couple of script includes and normal html/javascript.
From that tutorial, I was able to understand exactly what react is. Then I started adding in the other items on top.
It's definitely not a boilerplate "site in 5 minutes", but it's also a lot less confusing when trying to learn what's going on underneath the hood with react.
[0] - http://jamesknelson.com/learn-raw-react-no-jsx-flux-es6-webp...
I'm working on a book about learning React by itself, without Redux and Webpack and all the other tools that distract from learning the basics. Hope to have it out in the next week or so.
1- Create an app following the info here[0]. This was released officially few days ago, and it removes the pain of setuping React dependencies.
2- Use this [1] youtube tutorial to learn about React. I stopped at #9 since I didn't want to use Flux.
3- I think if you want to build something with React consider using Redux. Same youtube channel have a good tutorial[2] as well.
[0] - https://facebook.github.io/react/blog/2016/07/22/create-apps...
[1] - https://www.youtube.com/watch?v=MhkGQAoc7bc&list=PLoYCgNOIyG...
[1] "ReactJS for Stupid People" - this explains what React is in simple meaningful terms.
[2] "React.js Introduction For People Who Know Just Enough jQuery To Get By" - this shows you how things are usually done with just jQuery compared to how it's done in React. It really allows you to see why the jQuery way sucks.
[3] "Flux For Stupid People" - by reading this you'll understand what the Flux stuff is about and can decide whether you need it or not - I chose to skip it.
Finally, if you want to get up to speed with ES6 classes at the same time, [4] shows you a simple example. I personally started writing my React stuff this way straight away.
I'm liking React a lot at this point.
[1]: http://blog.andrewray.me/reactjs-for-stupid-people/ [2]: http://reactfordesigners.com/labs/reactjs-introduction-for-p... [3]: http://blog.andrewray.me/flux-for-stupid-people/ [4]: http://www.tamas.io/react-with-es6/
Beyond that, I maintain a big list of links to high-quality articles on React and related topics, at https://github.com/markerikson/react-redux-links . It's specifically intended to be a great place to start learning the ecosystem. Just for React, my list points to a couple dozen tutorial articles, a number of build-a-project tutorials, a number of articles that dig into how React works internally, and several paid courses and full books ( https://github.com/markerikson/react-redux-links/blob/master... ).
Thanks for sharing :)
If your component hierarchy matches your application state hierarchy, don't use flux. If you need to pass pieces of state around to a bunch of different components (like sharing state across views), use flux.
My first react application was complicated enough (enterprise analytics application) to require a flux implementation. I went redux and would highly recommend it both due to its ease-of-use and popularity. However, in subsequent applications I haven't needed it and have enjoyed writing vanilla react.
https://github.com/erikras/react-redux-universal-hot-example Universal Web Rendering. React + Redux + Express + ES6 + Webpack https://github.com/este/este For Web & Mobile (React Native, Redux, ES6...).
var enhanceComponent = (Component) =>
class Enhance extends React.Component {
render() {
return (
<Component
{...this.state}
{...this.props}
/>
)
}
};
Shouldn't `{...this.state}` be considered an anti-pattern?While i have read the oft-presented reasons for not using `this.state`, the arguments feel more academic to me than being based in practical examples. Your milage may vary, but in my experiences `this.state` is one of the more powerful features in React. To me, removing read access to component state also removes one of the more compelling reasons to include React into your codebase at all.
How long has React been out? Simple patterns such as component communication are debated, and third-party documentation about them is newsworthy?
But, in such an environment, there's no marketplace where ideas can compete and evolve. There's a trade-off where you have to do the hard work of navigating and choosing among competing ideas, but the benefit of React is that you can choose an architecture, today, that isn't a four-year-old snapshot of a small group's idiosyncratic architectural preferences.
I don't see that as a benefit... you must choose, and hope you chose well, based on poorer information and docs. It's an expense and a risk.
Some are more confident about the cost and risk than others, but less conscious of it I presume.
The bad thing about React is that you need to choose something that works for your team in your situation.
Its the frontend after all... what matters is UI and features. Component communication patterns and all this low level stuff is time lost.
I'd say really good advice I wish I had known was don't start with a flux implementation. Build out your app with standard react state, use state only at last cost (ie derive logic off of props or whatever else via functions before storing state data), and only when you get into a bit of mess with a really large application and too much difficulty deciphering what components are providing logic and how they interact with each other should you implement a flux.
Also, it's worth watching Dan Abramov's learning redux course on egghead just for how it gets you to think about react, javascript, and GUI development in general.
Later, when you do a real project, Redux makes debugging and state consistency about 10 times easier, especially on a team or coordinating between multiple teams.