Skip to content

Commit

Permalink
perf(native): improve performance by removing the internal state (#193)
Browse files Browse the repository at this point in the history
* perf(native): improve perfomance removing internal state

related #178 related #187

* test(native): update snapshots
  • Loading branch information
danilowoz committed Apr 8, 2020
1 parent 5613998 commit ab22b9c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 90 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "react-content-loader",
"version": "5.0.1",
"description": "SVG-Powered component to easily create placeholder loadings (like Facebook cards loading)",
"repository": {
"type": "git",
Expand Down
47 changes: 21 additions & 26 deletions src/native/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import Svg, {

import uid from '../shared/uid'
import { IContentLoaderProps } from './'
import offsetValueBound from './offsetValueBound'

class NativeSvg extends Component<IContentLoaderProps, { offset: number }> {
const AnimatedLinearGradient = Animated.createAnimatedComponent(LinearGradient)

class NativeSvg extends Component<IContentLoaderProps> {
static defaultProps = {
animate: true,
backgroundColor: '#f5f6f7',
Expand All @@ -22,8 +23,6 @@ class NativeSvg extends Component<IContentLoaderProps, { offset: number }> {
style: {},
}

state = { offset: -1 }

animatedValue = new Animated.Value(0)

fixedId = this.props.uniqueKey || uid()
Expand All @@ -50,18 +49,6 @@ class NativeSvg extends Component<IContentLoaderProps, { offset: number }> {
componentDidMount = () => {
if (this.props.animate) {
this.setAnimation()

this.animatedValue.addListener(({ value }) => {
this.setState({
offset: value,
})
})
}
}

componentWillUnmount = () => {
if (this.props.animate) {
this.animatedValue.removeAllListeners()
}
}

Expand All @@ -75,9 +62,17 @@ class NativeSvg extends Component<IContentLoaderProps, { offset: number }> {
...props
} = this.props

const offset1 = offsetValueBound(this.state.offset - 1)
const offset2 = offsetValueBound(this.state.offset)
const offset3 = offsetValueBound(this.state.offset + 1)
const x1Animation = this.animatedValue.interpolate({
extrapolate: 'clamp',
inputRange: [-1, 2],
outputRange: ['-100%', '100%'],
})

const x2Animation = this.animatedValue.interpolate({
extrapolate: 'clamp',
inputRange: [-1, 2],
outputRange: ['0%', '200%'],
})

const rtlStyle: object = rtl ? { transform: [{ rotateY: '180deg' }] } : {}
const svgStyle = Object.assign(Object.assign({}, style), rtlStyle)
Expand All @@ -101,17 +96,17 @@ class NativeSvg extends Component<IContentLoaderProps, { offset: number }> {
<Defs>
<ClipPath id={this.idGradient}>{children}</ClipPath>

<LinearGradient
<AnimatedLinearGradient
id={this.idClip}
x1={'-100%'}
x1={x1Animation}
x2={x2Animation}
y1={0}
x2={'100%'}
y2={0}
>
<Stop offset={offset1} stopColor={backgroundColor} />
<Stop offset={offset2} stopColor={foregroundColor} />
<Stop offset={offset3} stopColor={backgroundColor} />
</LinearGradient>
<Stop offset={0} stopColor={backgroundColor} />
<Stop offset={0.5} stopColor={foregroundColor} />
<Stop offset={1} stopColor={backgroundColor} />
</AnimatedLinearGradient>
</Defs>
</Svg>
)
Expand Down
32 changes: 16 additions & 16 deletions src/native/__tests__/__snapshots__/snapshots.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ exports[`ContentLoader snapshots renders correctly the basic version 1`] = `
</ClipPath>
<LinearGradient
id="snapshots-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -72,11 +72,11 @@ exports[`ContentLoader snapshots renders correctly the basic version 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down Expand Up @@ -146,8 +146,8 @@ exports[`ContentLoader snapshots renders correctly with viewBox defined 1`] = `
</ClipPath>
<LinearGradient
id="snapshots-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -156,11 +156,11 @@ exports[`ContentLoader snapshots renders correctly with viewBox defined 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down Expand Up @@ -230,8 +230,8 @@ exports[`ContentLoader snapshots renders correctly with viewBox defined and size
</ClipPath>
<LinearGradient
id="snapshots-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -240,11 +240,11 @@ exports[`ContentLoader snapshots renders correctly with viewBox defined and size
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down Expand Up @@ -314,8 +314,8 @@ exports[`ContentLoader snapshots renders correctly with viewBox empty 1`] = `
</ClipPath>
<LinearGradient
id="snapshots-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -324,11 +324,11 @@ exports[`ContentLoader snapshots renders correctly with viewBox empty 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
14 changes: 0 additions & 14 deletions src/native/__tests__/offsetValueBound.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ exports[`BulletListStyle renders correctly 1`] = `
</ClipPath>
<LinearGradient
id="BulletListStyle-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -84,11 +84,11 @@ exports[`BulletListStyle renders correctly 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ exports[`CodeStyle renders correctly 1`] = `
</ClipPath>
<LinearGradient
id="CodeStyle-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -88,11 +88,11 @@ exports[`CodeStyle renders correctly 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ exports[`FacebookStyle renders correctly 1`] = `
</ClipPath>
<LinearGradient
id="FacebookStyle-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -72,11 +72,11 @@ exports[`FacebookStyle renders correctly 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ exports[`InstagramStyle renders correctly 1`] = `
</ClipPath>
<LinearGradient
id="InstagramStyle-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -61,11 +61,11 @@ exports[`InstagramStyle renders correctly 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ exports[`ListStyle renders correctly 1`] = `
</ClipPath>
<LinearGradient
id="ListStyle-diff"
x1="-100%"
x2="100%"
x1="-33.33333333333334%"
x2="66.66666666666666%"
y1={0}
y2={0}
>
Expand All @@ -80,11 +80,11 @@ exports[`ListStyle renders correctly 1`] = `
stopColor="#f5f6f7"
/>
<Stop
offset={0}
offset={0.5}
stopColor="#eee"
/>
<Stop
offset={0}
offset={1}
stopColor="#f5f6f7"
/>
</LinearGradient>
Expand Down
13 changes: 0 additions & 13 deletions src/native/offsetValueBound.ts

This file was deleted.

0 comments on commit ab22b9c

Please sign in to comment.