Twitter | Search | |
Satyajit Sahoo
When building components, boolean props may not be always the best idea. For example, say you have a button with a primary prop (for primary color), and in future, you needed another color (accent), it's now possible to have a combination of props which doesn't make sense.
Reply Retweet Like More
Satyajit Sahoo 7 Jul 18
Replying to @lazarljubenovic
Not everyone uses a static type checker. Despite the additional complexity in writing the types, it's fine in your own projects, but if you're making a component library, you shouldn't rely on a static type checker for correct use of your API.
Reply Retweet Like
Alejandro [...πŸ‘¨πŸ»β€πŸ’»] 6 Jul 18
Replying to @satya164
My thinking on that is: make it simple first, if you need more variants, then go for another approach.
Reply Retweet Like
Satyajit Sahoo 6 Jul 18
Replying to @aganglada
For prototyping, yes. Otherwise changing the API of one heavily used component can result in touching a lot of places which is often not good.
Reply Retweet Like
Andrey Los πŸ‡ΊπŸ‡¦πŸ‡΅πŸ‡± 6 Jul 18
Replying to @satya164
I think programmers are smart enough to not be that dumb to use both of them :D So I don't see any issues with this approach. Although color props with enum is a bit better and safer, yes, but looks uglier a little.
Reply Retweet Like
Satyajit Sahoo 6 Jul 18
Replying to @rip212
No matter how smart, everyone makes mistakes :) Also, don't forget beginners who are mostly copy/pasting things and learning.
Reply Retweet Like
Manuel Bieh 🐝 6 Jul 18
Replying to @satya164
So? What's the problem? Define priorities. If `primary` AND `secondary` (or `accent`) is set, `primary` always wins. I see no problem here. You can also combine them. E.g. `<Button primary outlined />` - and you get an outlined button in the "primary" colors.
Reply Retweet Like
Satyajit Sahoo 6 Jul 18
Replying to @ManuelBieh
If your code needs additional context (such as priorities) to understand what it does, it's not a good API. A good API should prioritize clarity over terseness. Combining primary and outlined is not a problem here. It's about props which control the same aspect.
Reply Retweet Like
Abhimanyu rathore 6 Jul 18
Replying to @satya164 @semanticui
1. booleans are good as long as you use them for unique properties 2. for properties such as color- string or enum(if typescript) will be better choice Below snapshot is taken from react type definition
Reply Retweet Like
Satyajit Sahoo 6 Jul 18
The `disabled` prop might be a better example here. You know you will never need another state other than disabled and not disabled. But for shape, you might actually want a different one, eg, MD docs have a diamond shape. Though it's impossible to predict everything in advance.
Reply Retweet Like