Skip to content

Commit

Permalink
perf(blockcontainer): memo blocks inside of container to reduce rende…
Browse files Browse the repository at this point in the history
…rings
  • Loading branch information
schettn authored Sep 9, 2021
1 parent 91a36ac commit db60407
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
30 changes: 30 additions & 0 deletions packages/jaen-pages/src/containers/blocks/BlockItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {SFBWrapper} from '@snek-at/jaen-shared-ui'
import BlockProvider from '@src/contexts/block'
import {JaenBlock} from '@src/types'
import isEqual from 'lodash/isEqual'
import * as React from 'react'

type BlockItemProps = {
// block props
containerName: string
position: number
blockName: string
// other props
BlockComponent: JaenBlock
isEditing: boolean
onDelete: (position: number) => void
}

const BlockItem = React.memo<BlockItemProps>(
({children, BlockComponent, isEditing, onDelete, ...blockProps}) => {
return (
<BlockProvider {...blockProps}>
<SFBWrapper onDeleteClick={() => onDelete(blockProps.position)}>
<BlockComponent />
</SFBWrapper>
</BlockProvider>
)
}
)

export default BlockItem
32 changes: 13 additions & 19 deletions packages/jaen-pages/src/containers/blocks/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {withRedux} from '@store/withRedux'
import React, {useEffect, useState, useRef, useMemo} from 'react'
import {useCallback} from 'react'

import BlockItem from './BlockItem'

type BlockContainerProps = {
name: string
displayName: string
Expand All @@ -39,6 +41,8 @@ const BlockContainer: React.FC<BlockContainerProps> = ({
}) => {
const dispatch = useAppDispatch()

console.log('RERENDER container')

const {jaenPageContext} = useTemplate()
const pageId = jaenPageContext.id

Expand Down Expand Up @@ -153,38 +157,28 @@ const BlockContainer: React.FC<BlockContainerProps> = ({
typeName: string
): JaenBlock | undefined => blocks.find(b => b.BlockName === typeName)

const renderedBlocks = visibleBlocksKeys.map(position => {
const renderedBlocks = visibleBlocksKeys.map((position, key) => {
const BlockComponent = getBlockComponentByTypeName(
allBlocks?.[position].typeName
)

const numPosition = parseInt(position)

if (BlockComponent) {
const block = (
<BlockProvider
key={position}
return (
<BlockItem
key={numPosition}
containerName={name}
position={numPosition}
blockName={BlockComponent.BlockName}>
{!isEditing ? <BlockComponent /> : <BlockComponent />}
</BlockProvider>
blockName={BlockComponent.BlockName}
BlockComponent={BlockComponent}
isEditing={isEditing}
onDelete={deleteBlock}
/>
)

if (isEditing) {
return (
<SFBWrapper onDeleteClick={() => deleteBlock(numPosition)}>
{block}
</SFBWrapper>
)
}

return block
}
})

console.log('ADJAKDAJKD', React.Children.toArray(renderedBlocks))

if (isEditing) {
return (
<SFWrapper ref={ref} displayName={displayName} blockTypes={blocksTypes}>
Expand Down

0 comments on commit db60407

Please sign in to comment.