ReactJS Tutorial# 6 – ReactJS Best Practices

Remember, clean code is a consistent style of programming which makes your code much easier to write, read, and maintain. After few tutorials on ReactJS, we will take a look on some of the ReactJS Best Practices and Libraries that will help you write clean and efficient code.

You can refer the previous articles below-

Tutorial Index

  1. ReactJS Tutorial# 1 – An Introduction
  2. ReactJS Tutorial# 2 – Components and Props
  3. ReactJS Tutorial# 3 – Component State and Lifecycle
  4. ReactJS Tutorial# 4 – Forms, Events, and Keys
  5. ReactJS Tutorial# 5 – React Flux and Redux
  6. ReactJS Tutorial# 6 – ReactJS Best Practices
  7. 51-important-reactjs-interview-questions

Very often developers spend time on a problem, and of course, once the problem is solved, or at least they think it’solved, they make a pull request so it can be review and merged to the main branch. Well, you are not done just because your code “works”. Once it works, it is time and a chance to clean it up by removing your dead code (or zombie code), do little refactoring, and of course removing any commented-out code! Always strive for maintainability. And always ask yourself – “Is someone else going to be able to understand this code six months from now?”

Long story short, write code that you would be proud to take home and show your family.

So when we are talking about React and building Front End Application, we must mention that because it is a library and only handles the View, it offers a lot of different ways to use it and build applications. Let mention some important patterns and libraries which are good to be used with React for building amazing, fast and maintainable Front End Applications.

Redux and Redux-saga

As you already know Flux is the application architecture that Facebook uses for building client-side web applications. It complements React’s composable view components by utilizing a unidirectional data flow. For more information regarding this check this article.

Well if we have Redux and we know how to use it then why we need something more. What is this Redux-saga?

Well, Redux doesn’t handle “side-effect” very good. This means that asynchronous calls to APIs, file reads, etc should be handled with some other library, not Redux. So Redux-saga takes the power of the latest trends in JavaScript like Generators and yield and makes calling “side effects” code much cleaner. Sagas are almost same as functions. You don’t trust me? Check this code.

It is a function with an asterisk also known as a generator function. For more information about Redux-saga you can check their official documentation – https://redux-saga.js.org/ and for Generator functions, you can check this link – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function

Storybook

It is not only for React, but trust me, if you are building a bigger project and want to have really good structured components then you must check this library for UI Development Environment – https://storybook.js.org/

This UI tool enables developers to create components independently and also different showcase for each component. This is all done in an isolated development environment. Storybook runs outside of the main application so developers can easily develop UI components in isolation without worrying about application-specific dependencies and requirements. Amazing tool for working in teams.

Immutable.js

If you are quite a time developing in React and using Flux with Redux then you have seen somewhere Immutable for sure or at least someone told you to use ImmutableJS. And maybe you even open the documentation for it – https://facebook.github.io/immutable-js/ . Did you get confused?

Well, first you should know that in object-oriented and functional programming, an immutable object (also known as an unchangeable object) is an object whose state cannot be modified after it is created. And this is a contrast to a mutable object (changeable object), which can be modified after it is created.

Second, you need to make difference between normal JavaScript and how are some things used in ImmutableJS. For example, in normal JavaScript we know two common data types: {} Object and [] Array. This two become Map({}) and List([]) in ImmutableJS.

For example, to create Object in Immutable you should follow this approach:

Styled-components

Let’s say you want to create a simple and reusable <Button /> component that you can use in different places in your application. And of course, we like to have a normal version and a big and primary version for the important buttons. Also if you like to use JavaScript as CSS, which is the pattern that React is following with HTML and JSX then Styled-components is something that you should definitely check and use in your next project. After installing and importing the library the code will look something like this :

For more information you can check this link – https://www.styled-components.com/

Reactstrap

I have heard that you like Bootstrap. Well, most of the people are using it as their first choice. In case you want to use it with React and using the style of React then you should use Reactstrap which is in one sentence – Bootstrap 4 for React. Let’s show a simple example of how Bootstrap button will be used with Reactstrap.

For more information you can check this link – https://reactstrap.github.io/

Conclusion

A library is just one part of your application, so when you are using it, it makes sense the learning curve to be small, and as additional you can blend in some other libraries you might wanna use.

This is not a case when you have to use a framework. It is big and you are to adhere to their standard way of doing things.

Long story short, when you are using a framework, you are constrained to do everything their way, and with the library, you are on your own to discover what you might wanna use for doing HTTP requests or subscribing to key bindings. You can choose if you want to use either Redux or Flux for data store and state management or something else. You have freedom.

Tutorial Index

  1. ReactJS Tutorial# 1 – An Introduction
  2. ReactJS Tutorial# 2 – Components and Props
  3. ReactJS Tutorial# 3 – Component State and Lifecycle
  4. ReactJS Tutorial# 4 – Forms, Events, and Keys
  5. ReactJS Tutorial# 5 – React Flux and Redux
  6. ReactJS Tutorial# 6 – ReactJS Best Practices
  7. 51-important-reactjs-interview-questions

Add a Comment

Your email address will not be published. Required fields are marked *