USWDS - Character Count: Remove status message elements when .off() is called
#6387
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.
Summary
This PR adds a
teardown()method to remove the status message elements that are added to the DOM when.on()is called for the character count module.Breaking change
This is not a breaking change.
Related issue
Closes #6386
Problem statement
The character count module adds two status elements (one for visual, one for screen reader) to the DOM via
createStatusMessages()oninit(). However, it does not remove these elements when.off()is called.This prevents this module from being used properly in React's strict mode because rendering and callback refs run twice. Since
.on()gets called twice, and.off()(in a cleanup function) does not remove the status elements, you end up having 4 total status elements in the DOM (instead of the intended 2 elements).Solution
This PR adds a
teardown()method to the character count module that matches on theCHARACTER_COUNTclass at the specifiedroot, and queries for the elements there were added bycreateStatusMessages()and removes them.Let me know if you all think there is a better way!