- vừa được xem lúc

ReactJS | Why super(props) and bind() in constructor

0 0 33

Người đăng: Lan Truong

Theo Viblo Asia

This article tries to answer some WHY questions making me confused at the first time that I learn ReactJS about super(props) and bind method in constructor.

This post just explain some lines of code based on JavaScript knowledge. I do not go to deep theories, therefore, you need to read it through recommended articles below in case you are not familiar with these definitions before.

Requirements knowledge:

1. Why should we bind methods in constructor?

image.png

As we have already known:

this in a normal function is determined by how it is called, no matter where it is written.

In the code above, handleRemoveAll() is fired by the click event of <button>. Therefore in this case, this is not point to class Options but to event.target.

For that reason, using bind method to force this on handleRemoveAll method points to the class Options.

2. Why do we have to call super() on constructor?

Some questions that spring to my mind when I see super() in constructor at the first time: 'Why do we call super() on constructor although everything seems okay without constructor before ?'

I start to search and common answers on Internet say that:

We cannot use this in constructor until super() is called

But WHY?

So let jump into this example with me.

Imaging that we can use this in constructor without calling super()

image.png => The code above is ok, everything works well.

But what happens when we access this.name in actionType method

image.png

Well, definitely it will come into error. Because Dog class does not have name property. We only can use this.name when call super first to inherit name property from parents class.

Therefore,

JavaScript enforces that if you want to use this in a constructor, you have to call super first

This makes sure the program run smoothly at all times.

3. Why 'props' is passed to super()?

Once again, the answer on the Internet is:

Passing props to super helps us use this.props in constructor, otherwise, it will become undefined

WHY? Why we can call this.props outer constructor but it turns into undefined inside the constructor?

On ReactJs Document:

The constructor for a React component is called before it is mounted (Inserted into the tree)

Therefore, this ensures this.props is set even before the constructor exits.


That's all I want to share with you in this post ?

Reference:

  1. Why Do We Write super(props)
  2. Why do we have to ‘bind’ our functions in React apps?

Bình luận

Bài viết tương tự

- vừa được xem lúc

"this" trong JavaScript

Bài viết gốc: https://manhhomienbienthuy.bitbucket.io/2016/Mar/28/understanding-and-mastering-javascripts-this-keyword.html.

0 0 23

- vừa được xem lúc

Những điều cần lưu ý và sử dụng Hook trong React (Phần 5)

V. Sử dụng useRef như thế nào cho đúng.

0 0 127

- vừa được xem lúc

7 Cách viết code React "clean" hơn

Mở đầu. Là nhà phát triển React, tất cả chúng ta đều muốn viết code sạch hơn, đơn giản hơn và dễ đọc hơn. 1. Sử dụng JSX shorthands.

0 0 188

- vừa được xem lúc

Create app covid-19 use Reactjs

Chào các bạn hôm nay mình sẽ chia sẻ với các bạn một app covid-19, để mọi người cùng tham khảo, tính năng của App này chỉ đơn giản là show số liệu về dịch Covid của các nước trên thế giới như: Số ngườ

0 0 39

- vừa được xem lúc

ReactJS Custom Hooks

ReactJS cung cấp rất nhiều cho bạn các hook mà bạn có thể sử dụng hằng ngày vào project của mình. Nhưng bên cạnh đó bạn có thể tự tạo ra các hook của riêng bạn để sử dụng, làm cho việc tối ưu hóa code

0 0 65

- vừa được xem lúc

3 cách để tránh re-render khi dùng React context

3 cách để tránh re-render khi dùng React context. Nếu đã từng sử dụng React context cho dự án của bạn, và gặp phải tình trạng các component con - Consumer re-render rất nhiều lần, thậm chí bị sai logi

0 0 25