I’ve spent the past few months learning React. I see it’s usefulness but in all honesty I’m not really a fan of how it works. If I were to be in the business of building large projects with millions of users and dozens of functions, sure I could see it being great. But that’s not what most of us do. We design small little apps that we can perform and handful of actions with.
If your intention is to become a front end developer I think there are better tools that React. But that’s a different topic all together.
The project I decided to create to show my abilities with React backfired on me big time. I attempted to make something that would be as simple as possible while still fulfilling the requirements. I decided to create a Fantasy die roller, to emulate polyhedral dice for role playing games. I designed it originally with 4 components; a roller, a result pane, a saved die pane and finally a dice tray to keep all the components and props in.
The roller was to take in user input on what kind of die was to be rolled and allow the user to name and save the roll if desired. When the roll is saved, it should check the name against the saved rolls and if the name doesn’t exist go ahead and POST to the API, ‘saving’ the die. If the name does exist, PATCH the id number attached to that name instead to update it.
The Saved die components job was to allow for the display of any rolls the user decided to save. It would have a list of these rolls and the option to either re-roll the same die or delete it from the list.
The results component was simply to display the results to the screen.
The Dice Tray component was to hold all the logic for handlers and rolling the actual dice.
After a white boarding session to plan it all out I got to work creating the frame of the site. I created all the components and hardcoded a render of their name to see where it would sit on the page. Then I began some basic CSS to align everything and put the components where they needed to be in relation to each other. During this process I decided to eliminate the Dice tray component all together. This project was small and rendering everything to the tray and then the tray to the App and then the App to Index.js seemed like unnecessary steps. I removed it and shifted all the functionality to the App component.
I began by getting the function for the actual dice together. That was simple enough. Then I had to pass props to the Result component to be displayed on screen for the user. Again, pretty straight forward. Next I worked out how the saved rolls could be handled, so I designed 4 handle functions, save, delete, update and roll handler. The first 3 making API calls to store and retrieve dice, the latter invoking the die roller. I added an additional function on the Saved dice component for Re rolling that grabbed the parameters for a saved roll in the API and passed them through the die roller function to recreate rolls. All of this took me about 10 hours.
The place I went wrong was in displaying the Saved rolls in the component. I invoked a side effect to call the API and map through all the results in the saved rolls component, placing them on the screen. It works for all cases, saving, deleting and updating. The issue I found is that it does not “react” the same way my results component does. That one changes everything based on the state of the result changes, the effect does not.
I spent another 20 hours over two weeks trying to fix it but could not get it ‘react’ when a state is changed. I’m at the point where I think useEffect was not the proper way to get the results I wanted in the first place. I attempted to pass several props as control parameters in useEffect but never got anything other than an infinite loop. The only way to avoid that is using [] as the argument.
In addition to that, I had another student spend at least 10 hours on trying to help me solve it. After several hours of both of us banging our collective head against the wall. It was time to give up. Since I already have 1 “reactive” component in the results pane, I opted to code around the problem and at the last minute created a callback function that gets invoked with the handler functions, this refreshes the page after the API call completes. Same result even though it totally misses the point or React.
Other than having to refresh the page to see the updates, it works fairly well and I hope to not only fix the bugs I still have but I’d like to add some additional functionality going forward.
Stretch goals:
- Have the results list continue to log previous rolls with the newest on top
- Add the ability to roll multiple dice at once i.e.. 2d6