Slide 1

Slide 1 text

JS/ TS/ React mistakes and how to avoid them

Slide 2

Slide 2 text

HypeTech Tech education and shaping ideas into hype products hypetech.io | reactweek.dev Marko Arsić Founder and CEO @ HypeTech Founder of HypeTech Education Lecturer @ ReactWeek.dev Independent Tech Consultant Helping companies set up teams and standardize the development process github.com/marsicdev

Slide 3

Slide 3 text

Each developer has to overcome troubles and obstacles That’s the job!

Slide 4

Slide 4 text

There are no exceptions here and each field is difficult and it seems rather hard to avoid mistakes at the very start.

Slide 5

Slide 5 text

General mistakes 🤕

Slide 6

Slide 6 text

We all fail to do this Planning Architecturing Documenting Testing Decoupling Automating

Slide 7

Slide 7 text

Not always, but more than often. It’s a fact !

Slide 8

Slide 8 text

JavaScript mistakes

Slide 9

Slide 9 text

Not understanding all ES features === vs == !! Optinals

Slide 10

Slide 10 text

Not understanding pure functions filter, map, reduce

Slide 11

Slide 11 text

Not using virtual properties in classes and private fields

Slide 12

Slide 12 text

Promise (chain, return types)

Slide 13

Slide 13 text

Some compare async/await with Promise and claim it is the next generation in the evolution of JavaScript asynchronous programming

Slide 14

Slide 14 text

async/await IS an improvement, but it is no more than a syntactic sugar, which will not change our programming style completely.

Slide 15

Slide 15 text

Async functions are still promises. You have to understand promises before you can use async functions correctly, most of the time you need to use promises along with async functions.

Slide 16

Slide 16 text

TypeScript mistakes

Slide 17

Slide 17 text

Not Enabling Strict Mode Without TypeScript strict mode on, the typing can be too lax, and that will make our codebase less type-safe. It will give the wrong impression, as some think that by adding TypeScript all typing issues are automatically fixed.

Slide 18

Slide 18 text

Redeclaring interfaces When typing component interfaces, it is common to need to have some different interfaces variants of the same type. Those can vary in one or two parameters. A common mistake is to manually redefine those variations. That will lead to: ● unnecessary boilerplate. ● multiple changes are needed if one property changes in one place, that change needs to be propagated to many files.

Slide 19

Slide 19 text

Not Relying on Type Inference TypeScript inference is one of the most powerful tools of this programming language. It does all the work for us. We only have to make sure that all the pieces add together with as little intervention as possible. A crucial operator to achieving this is the typeof.

Slide 20

Slide 20 text

Incorrect use of Overloading TypeScript supports overloading natively. That is great as it can improve the readability of our contracts. However, it is different from other typed overloading languages. There are scenarios where it might make our code more complex and verbose.

Slide 21

Slide 21 text

Using the Function Type TypeScript ships with the Function type. It is like using the any keyword but for functions only. Enabling strict mode won’t prevent us from using it. Function type: ● accepts any number and type of parameters. ● the return type is always any

Slide 22

Slide 22 text

Relying on a Third Party for Immutability TS provides all the necessary tooling to make sure we don’t mutate our objects. We don’t need to add heavy libraries like ImmutableJS to our codebase.

Slide 23

Slide 23 text

React mistakes

Slide 24

Slide 24 text

Decomposition / Decoupling

Slide 25

Slide 25 text

Decomposition / Decoupling

Slide 26

Slide 26 text

Modifying state directly

Slide 27

Slide 27 text

Setting new state that relies on the previous state without using a function

Slide 28

Slide 28 text

Forgetting that setState is asynchronous

Slide 29

Slide 29 text

Writing logic in components

Slide 30

Slide 30 text

Updatating state on unmounted component

Slide 31

Slide 31 text

Not creating enough components Creating to many components

Slide 32

Slide 32 text

Not handling errors

Slide 33

Slide 33 text

Not using prop-types when writing JavaScript

Slide 34

Slide 34 text

Not using absolute paths

Slide 35

Slide 35 text

Not using key on a listing component

Slide 36

Slide 36 text

Not using helper models, classes, functions or custom hooks (react-use)

Slide 37

Slide 37 text

Using X for absolutely everything / Using X unnecessarily

Slide 38

Slide 38 text

Not using Developer Tools

Slide 39

Slide 39 text

Summary

Slide 40

Slide 40 text

These guidelines are simple to follow and are meant to help you embrace ecosystem rather than fight it.

Slide 41

Slide 41 text

By applying these simple tips you will have a better, less verbose, and easy to maintain codebase.

Slide 42

Slide 42 text

Q & A As everything good in life, knowledge is great only when shared https://discord.gg/94uhCAkFKf

Slide 43

Slide 43 text

As everything good in life, knowledge is great only when shared hypetech.io/education

Slide 44

Slide 44 text