ActiveModal refactor idea
#1660
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR outlines a suggestion for solving the following problems:
ActiveModal.ActiveModalWithState, to find a better way to have stateful modals. Before that, the only way to have stateful modals was by abandoning the current component's slots interface and passing in everything as a singlebodycomponent (for an example, seeNotificationModal). However, @jacbn made the observation that the solution I implemented effectively broke React's rules of hooks, by passing a custom hook as a prop to a component. If hooks should never be conditionally called, then this is also against the rules, since the component that calls the custom hook has no way of knowing that the same thing is passed every time).This PR shows a potential resolution to both problems. Using the example of
AdaTeacherOnboardingModal, in this example I show a new way for creating ActiveModals. This still makes use of the Redux state, but rather than storing the modal's component parts directly on the state, the modal is registered as a single component inActiveModals.tsx, and the state only stores anid. Because the modal is now defined as a React component rather than a POJO, we're able to add state (as well as call other hooks) just by just relying on component composition. And because the modal itself is never pushed to the state, we no longer receive the warnings.Any thoughts? If we chose to adopt this pattern, I think it'd be good to rewrite existing modals to also work this way. I think this can be done in a single afternoon. We can also adopt this incrementally, and the PR also shows what incremental adoption might look like.
PS: please disregard the changes to
Overview.testandstore.ts, these just annoyed me but are irrelevant.PS2: It would have been nice to enforce that
modalRegistrycan only holdIModals, where aIModalis any component that ultimately renders aModal. Unfortunately, React components all just have the same return type (some tsx), and that's as much the type system sees, so this is not possible.