Open mattrheault opened 4 years ago
This is awesome! I never knew about redux before. I just watched a video on it and it seems perfect. Thanks @mattrheault !!!
RE: Priority & strategy for this refactor
IMO this should be a high-priority refactoring project because it will dictate much of how future features will be built. Plus, the longer we wait, the harder the refactor will be. Thankfully, we can tackle this in stages with small PRs targeting 1-component or action at a time.
I can get us started with the first few refactoring PRs. But we'll also want to make sure that any net-new components use redux instead of trying to handle the async logic themselves.
Instructions: Just an FYI!
Just food for thought - no action required 😄 👍
Describing the problem & solution
Hello CS320 friends!
With my first FYI post, I wanted to call-out a useful design pattern that will help keep your react components simple & focused on view-logic. Namely, there are a number of components that have async logic & state management in them.
Example - EditMajors.js
The problem: Components have a lot of responsibility already with declaring view states. When components try to also handle network IO + stateful updates, they get complicated fast. Plus, tightly coupling IO logic within components means that said logic cannot be re-used across different parts of your react app.
The solution: Using a flux model async work can be coded & tested in isolation from components, and thus can be easily re-used. Redux is by far the most popular flux-like implementation in react.
Implementing redux in react
Typically, a frontend framework like angular, or ember would have strong opinions on how something like this should work. But with react being a "batteries not included" framework, we have to stitch the pieces together ourselves.
With redux setup, the app might look a bit more like this:
actions/StudentActions.js
Actions dispatch updates to alert redux reducers of state changes.
reducers/StudentInfo.js
Reducers listen for dispatched updates, & adjust state accordingly.
components/StudentInfo.js
Our component now selects props from redux state, & hooks up the actions.