Intro
I’ve been working with React for over four years. During this time, I’ve formed some opinions on how I think applications should b...
For further actions, you may consider blocking this person and/or reporting abuse
I’ve done this a dozen times. I think you’re right to formalize it. Nice post.
But I tend to agree with some of the comments. One great thing about selectors in redux is the ability to easily derive state. And I think there is still a space for functions that derive react hooks state based on state like data and loading without having to store additional state. This is one reason I’ve been leaning on useReducer, because you can export selectors from the hook, too, since it has access to a whole sub-state object.
Thank you 🙏🏽
The first time I ran into this problem, I switched to using numbers. 0 = not started, 1 = in progress, 2 = ready, 3 = post loading processes, etc.
But then everytime I went back to look at my code a week later I forgot what the numbers meant and it was just a huge pain. Then I found out about enums.
I did the same! But like you said, having to remember what all the numbers represented was difficult.
I actually tried using constants. NOT_STARTED, LOADING, etc. But enums was still better because of auto-complete and working with interfaces. Taking an enum type as the status is just more descriptive than an int, whether it's written as a constant or otherwise lol
I disagree with this article. I dont think you should be using a loading state at all. If the data is null, you are loading. If an error is present, the loading failed. Using a state with an enum adds complexity because now the same information is relayed through 3 different sources; the error state containing the error message/response, the value or resource state that contains the actual data on success, and your handcrafted enum/boolean. You could make a case for computing the enum on the fly for the sake of easy switches but imho having an if return the errormessage component whenever the error state is not null and returning a loading component when the data state is null (in that order) presents far cleaner and maintainable components. This also discurrages people nesting things too heavily since a lot of developers would probably use a switch with this enum ...
I look forward to hearing peoples responses!
This is also an interesting take! So you're saying instead of storing a separate enum string value, just derive the status based on whether or not
data
orerror
are notnull
?The one case I could argue against this for is re-fetching. Say you have a list of data, and you'd like to re-fetch or fetch more...your original
data
wouldn't be null...therefore how could you indicate to the user that you were re-fetching / fetching more?I think the basic idea is good, but the implementation is lacking.
An tagged union that holds the error/value would be remove quite some moving parts.
Nice, you're the second person to mention tagged unions to me. Looks like I need to do some learning! Thanks for the feedback! 👍
Useful thing which i have learnt for the day !! Great post
Yes, that's true.
It's true for databases too.
If you want to store a data in a SQL database which has three states (True, False, Unknown), then you might think a nullable boolean column (here "my_column") is the right choice. But I think it is not. Do you think the SQL statement "select * from my_table where my_column = %s" works? No, it won't work since "select * from my_table where my_column = NULL" will never ever return a single line. If you don't believe me, read: Effect of NULL in WHERE clauses (Wikipedia). If you like typing, you can work-around this in your application, but I prefer straight forward solutions with only few conditions.
If you want to store True, False, Unknown: Use text, integer or a new table and a foreign key.
From my Guidelines: github.com/guettli/programming-gui...
Great comparison! Thank you for sharing this!
Hi, I am trying to use enums as state for a blockchain game of rock paper scissors.
But I'm not quite sure how to use it based on this. I get a parsing error:
Can I use the
export
in the same .js app. Or does it have to be imported?Tagged unions in TypeScript do the same thing, but also allow you to attach a response or error message etc. I love them in combination with switch statements and exhaustive function returns, but a lot of developers react negatively to exhaustive switch statements that handle every possible case. They see them as verbose. I see them as the compiler detecting not only existing logic bugs, but preventing future issues that can arise. Algebraic data types are truly amazing if you embrace them.
This sounds interesting. Do you have any examples of this pattern you'd like to share? I'd like to learn more!
I personally have not used tagged unions yet. I'll look into them!
Since these are mutually dependent state items, I'd use useReducer.
Or to make the code much cleaner, move all this into a separate hook that uses useReducer internally.
I agree! I mentioned in the post that I normally wound't use three separate
useState
calls - just wanted to illustrate the concept easily.I do something similar to what you're mentioning - which you can read about here if you're interested 👍
dev.to/farazamiruddin/opinionated-...
Source code ?
Here you go!
(codesandbox.io/s/opinionated-react...)
Read through the entirety of the series when I noticed how much thought was put into the project structure of the first series.
I was not disappointed. Please keep this up :)
Thank you so much! I'm working on my next set of posts currently :)
Short and straight to the point. Love it!
Thank you Mahmoud!
Loved the series and definitely learned a lot.
Saved for future reference.
Thank you very much.
Damn, I have to login to say thanks for you, great article! 🍔🧀