Hey! Avoid these React Native App Development Blunders

 

Is React Native A Healthy Investment For Mobile App Development? | Hacker Noon

With over 1.6k active contributors working to make React Native a success, the glitter of a bright framework future has not gone unnoticed by developers looking to make a name in the cross-platform world.

The demand of app development with react native is getting from the developers is not just the doing of the fact that the industry has grown to an extent where it is now comparing Native and React Native modes of development. The demand is also driven by the constant updates that makes the framework advanced.

This demand, though, can only go so far. Ultimately, on a ground level, developers will have to learn their way around the development process. Especially on the front of understanding and avoiding React native app development mistakes which can ruin the users experience and their skills.

We have divided the article into four sections to ease the process of getting accustomed with the errors occured during React Native app development and know what it would take to become a developer that commits minimal mistakes.

Why do Developers Love React Native?

1. Because React Native uses JavaScript

Online javascript tutorial for beginners step by step

JavaScript is one of the languages which a developer starts their career with. And so they have a very detailed resemblance with the subject matter. The fact that React Native relies heavily on JavaScript brings down the learnability curve to a great extent.

2. They get to build native applications

10 Benefits of Native Mobile App Development

Even though the developers work on JavaScript, they don’t use web view for rendering React Native components. The one reason why you should choose React Native is because it renders real native elements, giving developers the flexibility to use platform specific components such as activity indicator, which gives very platform a recognizable, native-like* feel.

*It is impossible for a cross-platform developer to build fully native applications. The difference will be extremely visible on the UI and UX front.  

3. A straightforward installation process

How To Install WordPress | Web development design, WordPress web design, Web design

The installation method must always be direct, i.e. minus any complicated steps. The React Native framework can be installed through the Node Package Manager, which is extremely easy for developers with JavaScript background to follow. And even if you lack the understanding of JS, React Native will prevent you from the hassle of downloading binary from the source.

4. Greater productivity

The Secret to Greater Productivity

The key to building app faster lies in the developers’ productivity levels. While the reliability on JavaScript already makes the process easier, it also comes with a live reload feature and the freedom to use any IDE or text editor of their choice.

5. Strong community backing

Platform Engineering as a (Community) Service

React Native has gained a lot of popularity ever since its launch in 2015. Developers and businesses from all across the globe and industrial background have chosen to go with React Native at the back of the plethora of benefits it offers.

This high demand has in turn resulted in the creation of a strong widespread community working on adjusting the React Native pros and cons on a daily basis.

6. It is truly cross-platform

Best 10 Cross-Platform App Frameworks to Consider in 2020

When we say React Native is a cross-platform app development framework, we don’t just mean using React Native for mobile app development. Using React Native, developers get to expand their skills to smart TVs, smart watches, Windows devices, and even VR headsets.

Even amidst these and several other un-mentioned benefits of React Native development, there are some challenges that inherently come associated with React Native app development. Challenges that experienced developers have come to accept and work around. To eradicate the challenges, wherever you are located, be it Los Angeles, New York, etc, it is advisable to hire a company with app development with react native in New York to build and develop custom react native apps with the latest features.

The thing to note here is that these challenges that you are going to read next in no way imply that we are against React Native. It simply implies that like any other in the process to be perfect framework, there are some prevalent React Native limitations as well. There are in fact, several considerations to be made when developing React Native apps 

Challenges Associated with React Native App Development 

1. Dependency on Native app developers

Android Developers Blog: Native Dependencies in Android Studio 4.0

Noting how JavaScript works, developers sometimes have to rely on native developers as well. Especially when you have to put heavy computational operations in the application. These apps type force developers to offload computation to the native part of app, which in turn requires a native developer.

2. Limitations in Abstraction Layer

Promises and Limitations: Translating Microbiome Research to Practice - AGA Perspectives

In order to add more functionality in the React Native apps, an abstraction layer is added on native platform. Abstraction Layer in itself comes with some persistent issues – the issues to avoid in React Native app development – like:

  • Identifying bugs which gets encountered in the abstraction layer is extremely difficult.
  • High dependency on third-party services and libraries
  • Dependency on implementing custom designs

3. Zero support for multiprocessing or parallel threading

Multithreading VS Multiprocessing in Python | by Amine Baatout | Contentsquare Engineering | Medium

React Native comprises of a single Javascript thread because of which developers might observe slow performances when they want to execute any processes in parallel.

4. iOS deployment is particularly difficult

Apple reimagines the iPhone experience with iOS 14 - Apple

In case of React Native, developers find it particularly annoying when they decide to test their iPhone app on any testing services besides Apple’s Testflight, especially since they will have to deal with issues of obtaining provisioning profiles and necessary certificates.

The process is not very difficult on Android though, as it is very smooth and hassle-free to test and deploy Android apps.

As mentioned before, React Native, like any other framework doesn’t come free of challenges. It ultimately comes down to the experience of the developers. For a newbie or less skilled react native developers these challenges would appear as deal-breakers, while for skilled developers it won’t.

The only difference that stands between a skilled and unskilled React Native developer is the knowledge of mistakes that should be avoided in order to build an error free React Native app.

What makes React Native challenging for some is not just the inherent framework limitations but also the React Native app development mistakes that developers unknowingly commit. 

The 7 Common React Native App Development Mistakes

1. Wrong estimation

Missing the Target - The Friends of Israel Gospel Ministry

  • Layout for iOS and Android version – Yes there will be a number of reusable components, but there could also be different layouts. In fact, the mere structure of an application page can be different on iOS and Android completely.
  • Forms – you should estimate the validation layout as well. When you create app in React Native, you are supposed to write more codes compared to when you create a Hybrid app on suppose Cordova.
  • If creating Web app – You should check all the different endpoints which are offered by the backend. And because you will have to handle the logic in the app, it should all be coded correctly. You should understand the database structure, how the entities are connected, etc.

[ALSO READ: Top Local Databases for React Native App Development {A 2020 List} ]

2. Wrong redux store planning

The only introduction to Redux (and React-Redux) you'll ever need | by Hristijan Stevanoski | JavaScript in Plain English

As a developer when we get an innovative project, we focus more on planning the app layout and less on the data handling part of it.

Redux helps in storing the data correctly and managing, debugging app states. When planned right, it can be a powerful tool for managing the app data. When not, it can mess up many things.

Another point to not in case of Redux app development is that is not extremely suited for small projects. Even small changes will ask you to write long lines of codes. So, it’ll be better if you choose to go with it for large scale applications and avoid it when choosing React Native for startups.

3. Not reading external modules’ codes

Have you tried Pylance for VS Code? If not, here's why you should. | by SJ Porter | Towards Data Science

It is not uncommon for us developers to save time through external modules. It makes things easier and faster, especially since they come with documentation.

But, most often than not, modules break or they might not work as anticipated. This is the reason why developers should read the code and treat the step as one of the React Native best practices. Doing it helps to know what is wrong with the module and even how it can be solved.

4. Mutation of state inside render function

Upgrade/Migrate your Vue 2.x projects to Vue 3.0 | by Jonathan Ma | Medium

The image above highlights how the Datastore and View are interconnected. The datastore contains all your data in component and the view is rendered on the basis of state. It then consumes new state from data store and shows it on the screen.

In order to achieve that, React has a setState() function where the new object state is taken and compared to the previous state.

Finally, a new state is added after merger with the previous state and sent to the state datastore.

This cycle is available all throughout the lifetime of a component when you develop application in React Native.

Now, if you mutate the state directly, the lifecycle gets messed up and it corrupts all the previous states. This leads to app behaving abnormally or even crash. This will also result in you losing track of the states across component and will lead to you writing custom code in place of React. Additionally, you’ll end up having an unmanageable code and a heavy app.

5. Left “console.log” statements

JavaScript Basics: Console.log(). Today we are going to learn the basics… | by Erica N | Level Up Coding

Console log statements are extremely handy. They even provide help with debugging the app execution. But what happens when you leave the log statements in the app?

This can turn out to be a serious issue if you keep the render methods and logics inside, especially those which are asynchronous for they can lead to a bottleneck in JavaScript thread. All this ultimately leads to the application becoming slower.

6. Using stateless component for gaining React Native performance

Stateless Component vs Pure Component | by Vikas Singh | Groww Engineering

Some developers still think that what was true before React 16 is true even now.

Stateless component basically means that the component does not extend any class. It basically takes the argument as display and props in the DOM. It comes with benefits like –

  • Easy testing ability
  • Fast implementation
  • Does not use state or the local variables

With the time changing, it has today become wiser for developers to use pure component when they create React Native apps. Here’s why –

  • Performs shallow compare – This is a special win for complex UI applications as it lowers the render operations. The reason behind it is that it comes with a life cycle method known as shouldComponentUpdate which automatically does shallow comparison and then check if re-render is needed. In case of stateless component, re-render happens when parent component re-renders. But in the case of pure component, re-render only happens when a change is detected in the states or props.
  • Performs side effects – Developers can even send AJAX request inside componentDidmount or can perform some other DOM operations.

7. Not optimizing React Native images

React Native Image Performance. For the second installment of this… | by Adam Stanford | Medium

Optimizing the images in apps built with React Native should be a high priority task. It helps to resize the images locally and then uploading them to cloud storage like the s3 by server and getting the cdn link which then can be returned using API.

Following this process, helps make the image loading process fast.

While these are only 7, there can be a series of others as well. As a developer, your ultimate aim should be to make as few mistakes as possible.

Let us conclude the article by looking into what it would entail to become a developer who doesn’t commit these React Native app development mistakes.

How to Become a Mistakes-Proof React Native App Developer?

First thing first, there is not even a single developer on planet Earth who doesn’t commit mistakes.

Even developers with 10-15 years of experience commit mistakes. Your aim at the end of this article should not be to become a developers who doesn’t commit mistakes. It should be that you don’t commit the React native app development mistakes mentioned in the article and ones which are generally categorized as common in the mobile app development industry.

There are two ways to do that. Ways to become better React Native app developers –

A. Enroll in courses and keep brushing up your skills

Want to brush up your skills? This is how internships can kick-start your career - Education Today News

Enrolling in courses at the beginning of your career can be a great starting point on the present and future grounds.

Brushing up on the skill is equally necessary for when you are skilled and have years of experience. What happens is when you expand your career and work on a variety of projects, you most often than not lose sight of the technical understanding that you started with. Thus, it always comes across as a plus point to get back to the basics and revise things from scratch.

B. Associate with a company that focuses on training

Top 9 Human Resources Training Courses | AIHR Digital

This part is more valid for newbie developers compared to their experienced counterparts. When you are just starting your career you should associate yourself with a mid-sized company that has Team Leaders to hand-hold you through the practical application of what you learned on an academic level.

The reason I say mid-sized company is because a small sized company usually operates in a time crunch model, so will be working under the expectation that you will be on your A game since the day one. Considering this, it will always be better if you partner with a mid-size business who will focus on your learning.

Knowing the mistakes that can ruin the users experience can be a good starting point for developers who take their job seriously. And another good starting point would be to get associated with a mobile app development company that understands how mistakes are a part of learning and helps you grow.

If you are searching for a company in app development with react native in NewYork, California, Texas, and so forth, send in your queries to sales@appinventiv.com.

FAQs about React Native App Development Mistakes to Avoid

How to Write Effective FAQs: Complete With 10 Best Examples

Q. How do you improve react native performance?

There are multiple ways to improve the performance of your React native application:

  • Avoid unnecessary renders
  • Use PureComponent instead of Stateless
  • Optimize the JSON data
  • Lower the app size

Q. Why use Redux with react native?

Redux tool helps in storing  and managing the data, debugging app states. When planned right, it can be a powerful tool for managing the app data. Although considered beneficial, Redux is best suited for the development of complex applications as compared to simple apps, for the number of codes are higher.

 

 

Mobile Application Design Mistakes to Avoid

7 Common Mobile App Design Mistakes & Tips to Avoid Them

Mobile phones have contributed immensely to the progression of technological advancements. They’re no longer simple communication devices; they’re serving a pivotal role in business, entertainment, communication, and routine tasks of daily life. All this is possible because of mobile application. In the current era, we can easily find an app for every purpose – business, medical assistance, booking a ticket, paying online, games, education, cooking, shopping, or anything. You name it, and they have it. These apps help users from managing daily tasks to planning significant events; they also help promote business and ease communication between sellers and consumers.

The real problem

What's the Real Problem? - focushr

The real problem here is that users often don’t know what exactly they want. They are not aware of the concept of good application design, except the ones having slight knowledge of designing. One would never hear users praising the excellent design of an application; however, a poorly designed or poorly performing app would stick out like a sore thumb. So for a designer and a developer, the goal is to come up with an “unnoticeable design.”

On that note, let’s take a look at some of the most common mobile app design mistakes. That designers make, which causes an app to become negatively noticed by users.

Top mobile app design mistakes

1. A Poor First Impression

I Made a Bad Impression on a College Professor: 5 Things Freshmen Do That Make a Poor First Impression — Segue to College

The first look and appeal of an app are critical to attracting a potential user. A user makes a perception about the features and working of an application with his first experience of using it. If something seems confusing or dull, the user might not even give it a second try. Thus, the App Development Company must create an engaging first impression with a user-friendly UI.

Displaying relevant information on the first screen is very important. All the necessary icons like login, logout, the home page, help section, contact information, or any other important features should have their icons on the very first screen. Whatever the app is related to, its key functionalities should be reachable without any complexity.

Other than that, one prime factor that contributes to an excellent first impression is an app’s loading time. If it takes too much time to open the app or load any key feature, users get bored and lose interest. Thirdly, the colour scheme of an app should also align with its purpose. For example, an app used for professional purposes should not have a funky colour scheme, and entertainment-related apps should not be dull or boring. Colours should be bright and solid, or the users might get bored and have a terrible first experience.

2. Poor Information Architecture (IA)

Information Architecture for Web Design: the Big Picture | Brainhub

Most designers don’t spend enough time to design a proper information infrastructure for their app. That means the app should have easy access to information; this involves analyzing the most used or required features by users and making them visible in front. This concept is called “Prioritizing based on popularity.”

If you are developing an app with an already existing idea, it would be easier for you to identify the user’s priority by doing a little research. But if you are developing an app for a new business idea, you might not be aware of what users like most. So a designer should be capable of identifying this through his wisdom and experience, or they can release a prototype of the app and collect feedback from users. After that, they can implement the changes in their next release or update.

3. Lack of Design Consistency

Importance of Design Consistency. How creating Design Systems help solve… | by UXPin | Medium

Having a consistent design is a significant factor in designing an app’s UI. That means the font type should be the same within the whole app, the layout should be subtle, all icons should be placed correctly, and changing screens should change the visuals. Also, the text should be readable throughout the application.

Maintaining consistency throughout the app is the real trick for a designer. If needed, a little bit of inconsistency can be entertained if appropriately done; for example, highlighting some text or image, placing a great animation or advertisement. But these changes should align with the context of the app. Having a consistent design prevents users from getting confused and help in enhancing their experience.

4. Ambiguous CTA Positioning

Rich media ads: Best Practices For Creating CTAs That Demand Action

“Call To Action” buttons are placed on websites and apps to help and prompt users to take the next step. The positioning of CTAs on webpages not only affects lead capture for businesses but also directly impacts the user experience. To take full advantage of these buttons, designers must ensure that they correctly place them on the screen, and all their aspects are clearly defined. The action of these buttons is mostly mentioned through text placed strategically on them. Care should be taken to make the CTA text as clear and understandable for the user as possible.

5. Too Many Features

FeatureBloat2 | Connectionality

Having too many features within a single app is also not a suggested practice. It could make your app slow and congested, and it affects the overall performance of your app. It causes:

  • Complexity
  • A slow loading UI

Both of these issues could cause you the loss of potential users instantly. So it is best to have fewer features and serve them correctly. If having too many features is the requirement of your business, you can primarily offer a version with basic and primary functionality to gain the trust of users, and later, you can include new features in future updates.

6. Absence of Default Values

Technology's Role in Managing Employee Absences - Risk & Insurance : Risk & Insurance

Default values can save noteworthy client exertion in dull assignments, for example, filling in a similar form multiple times. Recognizing key values for form fields can build profitability and decrease annoyance. Your analytics can assist you with the comprehension if there is a most frequently picked choice for a particular field.

Specifically, dropdown menus profit by a relevant default. Numerous applications give “Choose One” as the default decision, compelling each client to communicate with the dropdown and select a value. On the contrary, if you preselect one value (the most used one), probably a few clients won’t need to associate with that dropdown at all.

7. Excessive use of Modals

Please, use modals wisely. - DEV Community

Numerous applications utilize modal windows to execute communications with information — altering a current thing, including another item, erasing, or in any event, perusing extra insights regarding an item. Modals show up over the current page and the background content is normally darkened (under the presumption that diminishing will decrease interruptions and assist clients with concentrating on the current task). Sadly, this design lessens the options for clients by concealing data that they may wish to allude to while filling the form. (Note that, regardless of whether the concealed window doesn’t contain data required for the editing, clients frequently endeavour to use the work they’ve done beforehand, by copying and pasting past sources of info.)

8. Irrelevant Information

Seven Ways to Overcome Information Overload | Insight BE

Long series of letters and digits, for example, consequently produced IDs in a database are as often used to exceptionally distinguish an item in an application. These strings are totally insignificant to clients, yet they are frequently shown as the primary section of a table, compelling individuals to check past that first segment to discover the data that they care about. While these good for nothing lists are significant towards the backend, they aren’t supposed to be shown to the users. Particularly in high–data density screens, give some comprehensible data as the primary stay point and push the IDs to a less visible position.

Why do users uninstall mobile apps?

Mobile app developers: Here's what uninstall rates can tell you about your product - TechRepublic

According to statistics, more than 91 billion apps were downloaded from Android and iOS app stores in 2017. It counts 13 apps per person on the entire planet. These stats have only increased since then, and users are getting dependent on these automated solutions. But it does not end here; research also shows that on average, users delete and app within 90 days of its download. Most apps are not opened more than two or three times within 30 days of downloading and are deleted without serving their purpose. The reason behind this is not the user. Users don’t download apps with the intention of not using them. Other factors frustrate them, and they get rid of apps. These factors mostly include poor performance and intricate app design. When an app doesn’t meet the user’s expectations, they switch to another one. And that’s because all that matters is comfort and ease for them.

That is why, while developing an application, it is essential to pay ample attention to its UI design. The goal should be to keep it as simple as possible, the colours should be soft and pleasant to the eyes, and the performance should be up to the mark. That would make an app popular among its users.

CONCLUSION

In 2020, uninstalls were still a pain for mobile apps | AppsFlyer

Despite the fact that the field of mobile application designing has progressed significantly from its initial arrangement, 2020 is expected to see an extraordinary increase in the number of both great and terrible applications. Indeed, even the apps which make it in 2020, there’s no assurance they will cut in 2021. It’s all about steady change. Designers need to continue updating their knowledge about user patterns and the user-psychology that drives these practices. The most important thing to be taken care of while designing an app is to think from a user’s perspective. Don’t choose or reject a colour, font, or design because you like or dislike it. Instead, research and go for what is trending. You can experiment by adding new things to your app to avoid mobile app design mistakes, but changing the whole designing rules is not recommended. It confuses users as they might have got along with the traditional practices, and your experiment could prove to be a big disaster. What works for others will undoubtedly work for you too.

Developing App? Here’s Angular Practices & Tips

Angular has been the most famous javascript framework among the developer community. It’s an MVC framework which provides pre-built components for developing the software application.Angular(2+) is based on Typescript , which is a superset of JavaScript. It comes with its most important advantage of static type checking that provides compile-time checking for any variable declarations and definitions.This blog is based around some best practices for Angular which I figured out while developing applications. There are also some bonus tips which I believe would help you with Angular development.

Essentials of mobile app development lifecycle that appreneurs must know - Business of Apps

Major Factors That Authenticate The Use Of AngularJS - Creative Tim's Blog

Follow the component based approach

Angular comes with the component-based paradigm which is also one of its best practices. It helps in maintaining modular and readable code.

While developing an angular code, if you think that something can be used multiple times as an independent piece of code, make it a component. A very basic example can be a simple dropdown which shows a list of options. The drop-down can act as an independent component with its own methods and template.

Few more examples could be – breadcrumbs for navigating throughout our web application, a simple alert box showing error/success messages or a loader. We tend to ignore these while thinking of modular approach but all these above examples can be thought of as independent components which can be reused over and over again.

Avoid using one huge global CSS

Angular’s current structure includes individual folder for every component. This folder includes-

  • .ts file for component logic
  • .html file
  • .css file for the component

So, keeping all of your CSS in one common CSS file would not only make your code less readable, it would make it less maintainable.

The best approach to style your components is to separate the global CSS with the local CSS (here local CSS refers to the component CSS). You should write your CSS in global CSS only when the CSS is written for the entire application and then if you need to style your component specific to a certain page, you can surely write in the local CSS.

Use CSS preprocessors for faster development

We can also use CSS preprocessors like SCSS/SASS for our Angular projects and I prefer them instead of writing plain CSS as they provide a lot of advantages over writing plain CSS. To mention one, the use of partials so that you can also separate your styles into multiple files for maintainability. If you guys want to know about partials, I would suggest you to go through this link.

You can divide your styling into multiple SCSS files just as we divide our application into multiple components. SCSS/SASS helps in writing smaller code which would ultimately get converted into CSS. So, why not save time and use it to our advantage.

CSS preprocessors | PSDtoWP.net

Save your time by using build tools

Tools like angular-cli have come up as a lifesaver for angular developers. It’s basically a boilerplate for an Angular application which helps in quickly setting up the angular application for your new project. It comes with all dependencies required for building your application and also has an inbuilt webpack which helps in bundling all your code and assets. Angular CLI helps to increase developers productivity through scaffolding by creating a component file, template file, stylesheet file etc. It comes with few basic commands which help in faster development like:-

  • ng g component my-new-component – it will create a new component with the name my-new-component.
  • ng g service my-new-service – it will create a new service with the name my-new-service.
  • ng g module my-module  – it will create a new module. A module is basically a collection of components which help to serve or attain a particular functionality.

Use ES6 paradigm and approach

Interfaces in Vanilla ES6 JavaScript – Full-Stack Feed

Though right now ES-8 is the current drafted version for ECMAScript but ES-6 came up with its own new features that are still widely used by developer community as they produce the same results with fewer lines of code and for best practices, every developer should know how to use them.

  • Arrow Function – In javascript, we use this keyword to refer to the current execution context. Previously, when we had to write javascript functions, this keyword would refer to the function context and we had to use a temporary variable to store the current execution context, so that we never lose it but with the arrow functions, the current execution context is never lost. For e.g., we could use an arrow function like this;

// ES5

var sum = function(x, y) { return x + y };

// ES6 (arrow function)

var sum = (x, y) => { return x + y };

Now, see the ES6 arrow function is just so compact and easy to understand and we can easily use this keyword inside this function to refer to the current execution context.

  • Template Literals – Template literals have come up with how we deal with printing strings having dynamic content.

For example, without template literals if we had to print a hello and goodbye message to a person, we would write something like :

const name = ‘AMAL’

console.log(‘Hello ’ + name +’ !’);

console.log(‘Goodbye ‘ + name + ‘ !’);

Now using template literals, it would be something like this:

const name = ‘AMAL’

console.log(`Hello ${name} !`);

console.log(`Goodbye  ${name} !`);

Now, who wouldn’t like to use the second syntax as it’s really easy to use and we don’t have to care about putting the spaces at right places so that the formatted output is correct.

Apart from these, const and let keywords came up as a replacement for global var variable so as to enable effective scoping of variables.

For a detailed overview of ES6 features, I would recommend having a glance over this.

Use Lazy Loading wherever possible

What is Lazy Loading | Lazy vs. Eager Loading | Imperva

Lazy loading is based around loading modules only when you want them to show.

We know about Angular’s modular architecture where a code is divided into modules with each module having its own components.

These components are basically views which might have some dynamic data. These components are rendered with the help of angular routing.

Lazy loading is one of the best things that angular framework comes with.

It’s based around one principle – ‘load/use it when you need it’. Oh, yeah what a lame
explanation I’ve given. There is a routing.ts file in every module which defines a URL for rendering each and every component.

Now when it comes to implementing lazy loading technique, what we do is we define a routing file for each and every module and we import each module only when the URL changes and it corresponds to rendering a component defined in the routing file of that module.

This technique requires a bit of patience to learn but take my words, it’s worth implementing. I can tell you with my personal experience; we reduced the loading time of our website by 3-5 seconds by implementing lazy loading.

Previously, we imported the whole bundle each and every time the user hit our website URL making our site slow while rendering. Then after implementing lazy loading, we only imported the module containing component required to render our index page and we just loaded other modules only based on what URL the user is switching to.

The Angular docs surely cover the lazy loading technique and it’s worth reading. You can check this link to learn more about it.

Follow DRY principle, extensively use services and directives

Services

DRY – This keyword is very famous in the developer community and it just stands for Don’t Repeat Yourself. So, when writing any code- be it Angular or any language- if you feel that you are basically writing or repeating the same code in every component, then you must pause then and there and rethink whether the code can be placed at a suitable place and can be shared by every component.

For example, if you want to call an API that updates user data, don’t call it in every component. What I would do it, I would rather make a service for it in the .service.ts file and would call that function whenever I need any component.

Also, it serves the purpose of using services in angular. Services help us in saving and fetching data and also help us write common functions which could be used by multiple components.

Directives

Now switching over to directives. A directive is a piece of code which is used to perform a specific task. Directives are of 3 types in Angular – component, structure, attribute.

  • Components are as we all know templates with logic to handle data.
  • Structure directives modify DOM by adding or removing an element.
  • Attribute directives change appearance or behavior of an element.

Once, I was wondering to restrict input elements to accept only numbers when a user types in for a phone number field which shouldn’t accept alphabets. For this, I built an only number directive which would only allow input elements to accept numbers through the keycode. So just by using this directive as an attribute on an HTML input element, I can modify/change its behavior.

Conclusion

Following best practice is never a compulsion. There are many ways of doing things and you may choose any path. However, using best practices mean that you are following a method or a technique which is tried and tested through experiments and has proved to be effective to attain desired results.

error: Content is protected !!