Enable selecting other language in modal popup #116
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.
Fixes #104
Summary
User can't select other language options in modal popup.
Root cause
Inside TranslatorProvider there is setLocaleToTranslateFrom. Calling that function (state setter) will cause TranslatorProvider to re-render.
Every time TranslatorProvider re-renders the localeOptions array changes reference.
useEffect has that localeOptions in deps, which means that useEffect callback will be ran every time
localeOptionschanges reference i.e. every time TranslatorProvider re-renders.Here in Content component, we can see that every time we click on one option from the dropdown
setLocaleToTranslateFromis called and therefore TranslatorProvider is re-rendered ->localeOptionschanges reference -> useEffect callback is called.Problem with useEffect callback is that it resets chosen locale to either defaultOption or to first locale in array.
In conclusion, every time we click on some option in the dropdown, the useEffect callback resets it to default locale or to first locale in
localeOptionsarray.Solution
Change deps of useEffect. useEffect callback can not be called every time we choose something from the dropdown. Instead of having
localeOptionsin deps, uselocalization.locales.That way, even when TranslatorProvider re-renders, the deps (localization.locales) will not change reference and
localeToTranslateFromwill not be reset. In addition, if locales change in payload config, useEffect callback will apply necessary changes.