Skip to content

Commit

Permalink
perf: tag list에 memo를 사용하여 최적화를 적용하라 (#613)
Browse files Browse the repository at this point in the history
- 홈 화면에서 필터링이 적용될 때 불필요한 tag 렌더링 최적화
- 글 작성 시 input 값이 변경될 때 불필요하게 tag가 리렌더링되는 곳 최적화 적용
- profile image에 memo 적용
  • Loading branch information
saseungmin authored Feb 9, 2023
1 parent 50db480 commit 10b2840
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/components/common/EmptyStateArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, ReactElement } from 'react';
import { ReactElement } from 'react';

import styled from '@emotion/styled';

Expand Down Expand Up @@ -46,7 +46,7 @@ function EmptyStateArea({
);
}

export default memo(EmptyStateArea);
export default EmptyStateArea;

const EmptyStateWrapper = styled.section<{ marginTop: string; }>`
display: flex;
Expand Down
4 changes: 2 additions & 2 deletions src/components/common/ProfileImage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from 'react';
import { memo, ReactElement } from 'react';

import Image from 'next/image';

Expand Down Expand Up @@ -50,7 +50,7 @@ function ProfileImage({
);
}

export default ProfileImage;
export default memo(ProfileImage);

const ProfileAvatarImage = styled(Image)`
border-radius: 70%;
Expand Down
24 changes: 13 additions & 11 deletions src/components/common/SwitchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ interface Props {
defaultChecked: boolean;
}

const SwitchButton = ({ onChange, defaultChecked }: Props) => (
<SwitchButtonWrapper
name="switch-toggle"
defaultChecked={defaultChecked}
title="switchButton"
aria-label="No label tag"
onChange={onChange}
icons={false}
data-testid="switch-button"
/>
);
function SwitchButton({ onChange, defaultChecked }: Props) {
return (
<SwitchButtonWrapper
name="switch-toggle"
defaultChecked={defaultChecked}
title="switchButton"
aria-label="No label tag"
onChange={onChange}
icons={false}
data-testid="switch-button"
/>
);
}

export default SwitchButton;

Expand Down
4 changes: 2 additions & 2 deletions src/components/common/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from 'react';
import { memo, ReactElement } from 'react';
import { X as CloseSvg } from 'react-feather';

import { useTheme } from '@emotion/react';
Expand Down Expand Up @@ -37,7 +37,7 @@ function Tag({ tag, onRemove, onClick }: Props): ReactElement {
);
}

export default Tag;
export default memo(Tag);

const TagWrapper = styled.div`
display: inline-flex;
Expand Down
14 changes: 8 additions & 6 deletions src/components/home/TagsBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from 'react';
import { memo, ReactElement, useCallback } from 'react';

import styled from '@emotion/styled';
import { useSetRecoilState } from 'recoil';
Expand All @@ -13,23 +13,25 @@ function TagsBar(): ReactElement {

const setGroupsCondition = useSetRecoilState(groupsConditionState);

const onClick = useCallback((name: string) => setGroupsCondition((prev) => ({
...prev,
tag: name,
})), []);

return (
<TagsWrapper>
{tags.map(({ name }) => (
<Tag
key={name}
tag={name}
onClick={() => setGroupsCondition((prev) => ({
...prev,
tag: name,
}))}
onClick={() => onClick(name)}
/>
))}
</TagsWrapper>
);
}

export default TagsBar;
export default memo(TagsBar);

const TagsWrapper = styled.div`
height: 36px;
Expand Down
4 changes: 2 additions & 2 deletions src/components/write/TagForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ function TagForm({ tags: initialTags, onChange }: Props): ReactElement {
actionKeyEvent(e, ['Enter']);
}, [actionKeyEvent]);

const handleRemove = (nextTags: string[]) => {
const handleRemove = useCallback((nextTags: string[]) => {
setTags(nextTags);
onChange(nextTags);
};
}, [onChange]);

useEffect(() => {
setTags(initialTags);
Expand Down
8 changes: 4 additions & 4 deletions src/components/write/TagList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from 'react';
import { memo, ReactElement, useCallback } from 'react';

import Tag from '../common/Tag';

Expand All @@ -8,11 +8,11 @@ interface Props {
}

function TagList({ onRemove, tags }: Props): ReactElement {
const handleRemove = (target: string) => {
const handleRemove = useCallback((target: string) => {
const nextTags = tags.filter((tag) => tag !== target);

onRemove(nextTags);
};
}, [tags, onRemove]);

return (
<>
Expand All @@ -27,4 +27,4 @@ function TagList({ onRemove, tags }: Props): ReactElement {
);
}

export default TagList;
export default memo(TagList);
2 changes: 1 addition & 1 deletion src/containers/write/WriteFormContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function WriteFormContainer(): ReactElement | null {

const onChangeFields = useCallback((form: Partial<WriteFields>) => {
changeFields((prevState) => ({ ...prevState, ...form }));
}, [changeFields]);
}, []);

if (!user) {
return null;
Expand Down

1 comment on commit 10b2840

@vercel
Copy link

@vercel vercel bot commented on 10b2840 Feb 9, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.