DEV Community

Discussion on: Introduction to JavaScript Property Getters and Setters (Accessor Properties)

Collapse
 
sametweb profile image
Samet Mutevelli

Thanks for sharing. I recently learned about getters/setters. While it is a good concept in terms of OOP, I wonder if there is a use case in the React ecosystem. I would like to hear your thoughts on that.

Collapse
 
unsignedmind profile image
unsignedmind • Edited

First: It's good to know how it works under the hood but today you can just use es6 classes and with typescript you dont need to check the type yourself. Just define the input a string.
Second: Whenever you need a a combination of values a getter is usefull. For example a component has 2 attributes. One const for MAX_ENTRIES and another one for an array entries. The getter would be smth like entriesLeft and returns MAX_ENTRIES - entries.length.