Skip to content

Commit

Permalink
fix(lag): memoize some components
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchann committed Dec 7, 2021
1 parent 5dfbcb7 commit 87b78a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import { connect, ConnectedProps } from 'react-redux'
import { equals } from 'ramda'

Expand All @@ -8,18 +8,16 @@ import { RootState } from 'data/rootReducer'
import { getData } from './selectors'
import Success from './template.success'

class CryptoItem extends React.Component<Props> {
shouldComponentUpdate = (nextProps) => !equals(this.props, nextProps)

render() {
return this.props.data.cata({
const CryptoItem = memo(
(props: Props) =>
props.data.cata({
Failure: () => null,
Loading: () => null,
NotAsked: () => null,
Success: (val) => <Success {...this.props} {...val} />
})
}
}
Success: (val) => <Success {...props} {...val} />
}),
(prevProps, nextProps) => equals(prevProps, nextProps)
)

const mapStateToProps = (state: RootState, ownProps: OwnProps) => ({
data: getData(state, ownProps)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import { equals } from 'ramda'
import styled from 'styled-components'

Expand Down Expand Up @@ -65,11 +65,10 @@ const PlusMinusIconWrapper = styled.div`

type Props = OwnProps & ParentOwnProps & SuccessStateType

const Success = React.memo(
const Success = memo(
({ coin, fiat, onClick, orderType, rates }: Props) => {
const { coinfig } = window.coins[coin]
const displayName = coinfig.name

return (
<CheckoutDisplayContainer
data-e2e={`sb${coin}-${fiat}CurrencySelector`}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo, useEffect } from 'react'
import { connect, ConnectedProps } from 'react-redux'
import { equals } from 'ramda'
import { bindActionCreators, Dispatch } from 'redux'
Expand Down Expand Up @@ -42,20 +42,18 @@ const getColorFromMovement = (movement: PriceMovementDirType) => {
}
}

class PriceMovement extends React.Component<Props> {
componentDidMount() {
if (!Remote.Success.is(this.props.data)) {
const { coin } = this.props
this.props.miscActions.fetchPriceChange(coin, this.props.fiat || 'EUR', TimeRange.DAY)
}
}

shouldComponentUpdate = (nextProps) => !equals(this.props, nextProps)
const PriceMovement = memo(
(props: Props) => {
useEffect(() => {
const { coin, data, fiat, miscActions } = props
if (!Remote.Success.is(data)) {
miscActions.fetchPriceChange(coin, fiat || 'EUR', TimeRange.DAY)
}
}, [props])

render() {
return (
<Container>
{this.props.data.cata({
{props.data.cata({
Failure: () => null,
Loading: () => <SkeletonRectangle height='12px' width='40px' />,
NotAsked: () => null,
Expand All @@ -68,8 +66,9 @@ class PriceMovement extends React.Component<Props> {
})}
</Container>
)
}
}
},
(prevProps, nextProps) => equals(prevProps, nextProps)
)

const mapStateToProps = (state: RootState, ownProps: OwnProps) => ({
data: getData(state, ownProps)
Expand Down

0 comments on commit 87b78a5

Please sign in to comment.