Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(localized-text-input): use hooks #958

Merged
merged 4 commits into from
Jul 29, 2019
Merged

refactor(localized-text-input): use hooks #958

merged 4 commits into from
Jul 29, 2019

Conversation

montezume
Copy link
Contributor

Summary

Refactors the LocalizedTextInput container to use react hooks


static isEmpty = isEmpty;
if (hasErrorInRemainingLanguages) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this replaces the getDerivedStateFromProps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing

event.target.language = this.props.language;
this.props.onChange(event);
event.target.language = props.language;
props.onChange(event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could useCallback with lang and onChange as dep.

defaultExpansionState = true;
} else if (props.defaultExpandLanguages) {
defaultExpansionState = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it would be easier to follow when inlining this at L129 and OR joining both prop values?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it can be as simple as:

const defaultExpansionState = props.hideLanguageExpansionControls || props.defaultExpandLanguages;

Copy link
Member

@emmenko emmenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see how the component gets simpler!


static getId = getId;
const toggleLanguages = React.useCallback(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm do we really need this callback? Can't we use the setter directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's a micro optimization. If the expansion state doesn't change, then the function can remove memoized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with removing it, just let me know 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s not a big deal, I personally don’t see a need of wrapping a setter with another setter, as there is no extra logic involved.


static isEmpty = isEmpty;
if (hasErrorInRemainingLanguages) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing


static omitEmptyTranslations = omitEmptyTranslations;
if (shouldLanguagesExpand !== areLanguagesExpanded) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the link to the docs here, otherwise it might be confusing to see updating the state on rendering.

Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧽

@emmenko emmenko force-pushed the ml-hook-refactor branch from a357c60 to 074c531 Compare July 29, 2019 08:06
event.target.language = props.language;
onChange(event);
},
[props.language, onChange]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆we only need to destructure onChange, for whatever reason. I suppose the lint plugin has some bugs.

@emmenko emmenko merged commit 06d43a3 into master Jul 29, 2019
@emmenko emmenko deleted the ml-hook-refactor branch July 29, 2019 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants