perf: add React.memo to hot-path components
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
|
import React from 'react'
|
||||||
import AppBar from '@mui/material/AppBar'
|
import AppBar from '@mui/material/AppBar'
|
||||||
import Badge from '@mui/material/Badge'
|
import Badge from '@mui/material/Badge'
|
||||||
import Box from '@mui/material/Box'
|
import Box from '@mui/material/Box'
|
||||||
@@ -30,7 +31,7 @@ type NavItem = { label: string; to: string }
|
|||||||
|
|
||||||
const navItems: NavItem[] = [{ label: 'Каталог', to: '/' }]
|
const navItems: NavItem[] = [{ label: 'Каталог', to: '/' }]
|
||||||
|
|
||||||
export function AppHeader() {
|
export const AppHeader = React.memo(function AppHeader() {
|
||||||
const { mode, resolvedMode, scheme, setScheme, cycleMode } = useThemeController()
|
const { mode, resolvedMode, scheme, setScheme, cycleMode } = useThemeController()
|
||||||
const user = useUnit($user)
|
const user = useUnit($user)
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { ReactNode } from 'react'
|
import type { ReactNode } from 'react'
|
||||||
import { useCallback, useMemo, useRef } from 'react'
|
import { useCallback, useMemo, useRef } from 'react'
|
||||||
|
import React from 'react'
|
||||||
import { useMediaQuery } from '@mui/material'
|
import { useMediaQuery } from '@mui/material'
|
||||||
import Box from '@mui/material/Box'
|
import Box from '@mui/material/Box'
|
||||||
import Card from '@mui/material/Card'
|
import Card from '@mui/material/Card'
|
||||||
@@ -18,7 +19,7 @@ import type { Swiper as SwiperType } from 'swiper/types'
|
|||||||
|
|
||||||
type Props = { product: Product; mediaHeight?: number; actions?: ReactNode }
|
type Props = { product: Product; mediaHeight?: number; actions?: ReactNode }
|
||||||
|
|
||||||
export function ProductCard({ product, mediaHeight = 200, actions }: Props) {
|
const ProductCardInner = ({ product, mediaHeight = 200, actions }: Props) => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const isMobile = useMediaQuery('(max-width:600px)')
|
const isMobile = useMediaQuery('(max-width:600px)')
|
||||||
const swiperRef = useRef<SwiperType | null>(null)
|
const swiperRef = useRef<SwiperType | null>(null)
|
||||||
@@ -247,3 +248,7 @@ export function ProductCard({ product, mediaHeight = 200, actions }: Props) {
|
|||||||
</Card>
|
</Card>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const ProductCard = React.memo(ProductCardInner, (prev, next) => {
|
||||||
|
return prev.product.id === next.product.id && prev.mediaHeight === next.mediaHeight && prev.actions === next.actions
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
import React from 'react'
|
||||||
import Box from '@mui/material/Box'
|
import Box from '@mui/material/Box'
|
||||||
import type { SxProps, Theme } from '@mui/material/styles'
|
import type { SxProps, Theme } from '@mui/material/styles'
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ function buildFallbackSrc(src: string, width: number): string {
|
|||||||
return `/uploads-resized/${pathPrefix}${parsed.uuid}.webp?w=${width}`
|
return `/uploads-resized/${pathPrefix}${parsed.uuid}.webp?w=${width}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function OptimizedImage({
|
export const OptimizedImage = React.memo(function OptimizedImage({
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
widths = DEFAULT_WIDTHS,
|
widths = DEFAULT_WIDTHS,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
|
import React from 'react'
|
||||||
import Avatar from '@mui/material/Avatar'
|
import Avatar from '@mui/material/Avatar'
|
||||||
import type { SxProps, Theme } from '@mui/material/styles'
|
import type { SxProps, Theme } from '@mui/material/styles'
|
||||||
import { createAvatar } from '@dicebear/core'
|
import { createAvatar } from '@dicebear/core'
|
||||||
@@ -12,7 +13,7 @@ type UserAvatarProps = {
|
|||||||
sx?: SxProps<Theme>
|
sx?: SxProps<Theme>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function UserAvatar({ userId, avatarUrl, avatarStyle, size = 40, sx }: UserAvatarProps) {
|
export const UserAvatar = React.memo(function UserAvatar({ userId, avatarUrl, avatarStyle, size = 40, sx }: UserAvatarProps) {
|
||||||
const [generatedSrc, setGeneratedSrc] = useState<string | null>(null)
|
const [generatedSrc, setGeneratedSrc] = useState<string | null>(null)
|
||||||
const styleId = avatarStyle || DEFAULT_STYLE_ID
|
const styleId = avatarStyle || DEFAULT_STYLE_ID
|
||||||
const styleIdRef = useRef(styleId)
|
const styleIdRef = useRef(styleId)
|
||||||
|
|||||||
Reference in New Issue
Block a user