React became an open-source project in 2013. Since then, it has evolved greatly. Searching the Internet, you can find old posts with different approaches. Here I will describe the eight key decisions that your team should support when writing to React.
Solution 1: development environment
Before you write your first component, your team needs to agree on a development environment. There are many options. ')
What tool do you usually use when developing on React?
You can ignore types, use PropTypes , Flow or TypeScript . Please note that since React 15.5 PropTypes is in a separate library , so the old posts on this topic are no longer relevant.
The community is shared in this thread:
To comply with the types in React, I usually use ...
I prefer PropTypes because I believe that it provides sufficient type safety in the React components. Using the combination of Babel, Jest tests , ESLint , and PropTypes, I rarely see problems like runtime.
Solution 3: createClass vs ES classes
React.createClass was a separate API, but at 15.5, it is outdated. Some people think that we are too far ahead of the ES classes . Despite this, the createClass function was removed from React and referred to a single chapter called “React without ES6” in the React documentation. And so it is clear: ES classes are future ones. You can easily convert code from createClass to ES classes using react-codemod .
Solution 4: Classes or Functions
You can declare React components through classes or functions. Classes are useful when you need references or lifecycle methods. Here are 9 reasons to use features whenever possible. But, also, it is worth noting that the functional components have their drawbacks.
Solution 5: States
You can use the standard Reakta State. It's enough. For scaling, you can use the lifting state . Or you can enjoy Redux or MobX.
Please, without controversy - honestly wondering what the React community is using these days.For new React projects I use ...
I like Redux , but I often use the standard React state when possible. At the moment we have released a bunch of React applications, and decided that Redux is the worst choice. I prefer to do many small standalone applications on top of one large application.
There are several ways to bind functions in React components. Modern JS offers enough ways to bind. You can bind functions in the constructor, in the renderer, use the function in the renderer, use class properties or decorators. See the comments in this post to see more features. Each approach has its advantages, you may like to use the experimental possibilities of the language, I personally suggest using the properties of classes .
This survey was in August 2016. Since then, class properties have become popular, and createClass has fallen in popularity.
Note: Many people are confused about why the arrow functions and the binding functions in the render cause problems. The real reason? This makes shouldComponentUpdate and PureComponent moody .
Solution 7: Styles
That's where the options really are many. There are 50+ approaches for writing styles to your React components, including traditional CSS, Sass / Less, CSS modules and 56 CSS-in-JS options. No kidding. I spent the statistics and this is what happened:
Red is bad.Green is good.Gray is dangerous.
See why there are so many items in the styling options. There is no clear winner.
How do you style your #reactjs application (answer with a specific library)?
You can see how CSS-in-JS is gaining momentum.Css-modules are losing momentum.
My team uses Sass with BEM and we are happy with it, but I also like the styled components .
Solution 8: Reusable Code
In React, there are so-called mixins - a mechanism for code reuse. However, currently, mixin is not recommended . You cannot use mixins with components of ES classes, because of this, people use higher order components and render-props (aka function as a child) to inherit code between components.
Survey for developers writing to #ReactJS: What do you prefer?
Higher order components are more popular now, but I prefer render-props since they are easier to read and create.