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

chore: migrate several class-based components to functional components #5584

Merged
merged 2 commits into from
Mar 20, 2024

Conversation

jsathu07
Copy link
Contributor

Proposed changes

The React team recommends replacing class-based components with functional components. This PR migrates several of them.

Issue(s)

How to test or reproduce

Screenshots

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Improvement (non-breaking change which improves a current function)
  • New feature (non-breaking change which adds functionality)
  • Documentation update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • I have added necessary documentation (if applicable)
  • Any dependent changes have been merged and published in downstream modules

Further comments

private animatedValue: Animated.Value;
const Dropdown = ({ currentDepartment, onClose, onDepartmentSelected, departments }: IDropdownProps) => {
const animatedValue = useRef(new Animated.Value(0)).current;
const { theme } = useTheme();
Copy link
Member

Choose a reason for hiding this comment

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

theme is deprecated on useTheme.
We've been using colors, just like

const { colors } = useTheme();

Comment on lines 44 to 45
const heightDestination = 0;
const maxRows = 5;
Copy link
Member

Choose a reason for hiding this comment

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

Constants should live outside the component, so they won't be recreated.

@@ -26,73 +26,75 @@ interface IDirectoryOptionsProps {
theme: TSupportedThemes;
}

export default class DirectoryOptions extends PureComponent<IDirectoryOptionsProps, any> {
private animatedValue: Animated.Value;
const DirectoryOptions = memo(
Copy link
Member

Choose a reason for hiding this comment

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

memo requires all props to be memoized in order to work properly.
By default on function components, functions like toggleWorkspace will be recreated on every render, which is going to make memo irrelevant here.
That said,
DirectoryView is a class component and toggleWorkspace uses arrow function, which makes it bound to the class and memo is going to end up working after all.

IMO we should remove memo from here, just because when we migrate DirectoryView to function component, we'll have to be remember to use useCallback on toggleWorkspace there in order to keep memo working here.
DirectoryOptions is not an expensive component, so I say we remove memo.

const subscription = useRef<Subscription>();
const newServerTimeout = useRef<ReturnType<typeof setTimeout> | false>();
const isMounted = useRef(false);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

const isMounted = useRef(false);

const [servers, setServers] = useState<TServerModel[]>([]);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

/>
);

const maxRows = 4;
Copy link
Member

Choose a reason for hiding this comment

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

Create as MAX_ROWS outside the component

const [servers, setServers] = useState<TServerModel[]>([]);

const dispatch = useDispatch();
const { theme } = useTheme();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const { theme } = useTheme();
const { colors } = useTheme();

]}
/>
</TouchableWithoutFeedback>
const statusBarHeight = insets?.top ?? 0;
Copy link
Member

Choose a reason for hiding this comment

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

Remove this const and use insets directly

Comment on lines 24 to 23
const Dropdown = ({ isMasterDetail, theme, currentFilter, onClose, onFilterSelected }: IDropdownProps) => {
const animatedValue = useRef(new Animated.Value(0)).current;
Copy link
Member

Choose a reason for hiding this comment

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

Remove theme prop and use useTheme

close: onClose,
changeType,
toggleWorkspace,
theme
Copy link
Member

Choose a reason for hiding this comment

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

Remove theme prop (on DirectoryView too) and use useTheme

@jsathu07
Copy link
Contributor Author

@diegolmello I have completed the requested changes

@jsathu07
Copy link
Contributor Author

jsathu07 commented Mar 7, 2024

@diegolmello can you check this pr in your free time, thanks

@diegolmello diegolmello merged commit a2975f7 into RocketChat:develop Mar 20, 2024
2 of 4 checks passed
@diegolmello
Copy link
Member

Merged. Thanks for your contribution, @jsathu07!

@jsathu07 jsathu07 deleted the migrate_component_functional branch March 20, 2024 14:27
@jsathu07 jsathu07 restored the migrate_component_functional branch March 20, 2024 14:56
@jsathu07 jsathu07 deleted the migrate_component_functional branch March 22, 2024 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants