Skip to content

Commit 463b134

Browse files
committed
fix: fix issue #19
Read props at render time, instead of in the constructor. That way if props change we're always reading the latest values.
1 parent b867091 commit 463b134

1 file changed

Lines changed: 17 additions & 26 deletions

File tree

src/index.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ class Zoom extends Component {
1313
mouseY: null,
1414
}
1515

16-
const {
17-
height,
18-
img,
19-
transitionTime,
20-
width,
21-
} = props
22-
23-
this.outerDivStyle = {
24-
height: `${height}px`,
25-
width: `${width}px`,
26-
overflow: 'hidden',
27-
}
28-
29-
this.innerDivStyle = {
30-
height: `${height}px`,
31-
backgroundRepeat: 'no-repeat',
32-
backgroundPosition: 'center',
33-
backgroundSize: 'auto 100%',
34-
transition: `transform ${transitionTime}s ease-out`,
35-
backgroundImage: `url('${img}')`,
36-
}
37-
3816
this.imageRef = createRef()
3917

4018
this.handleMouseOver = this.handleMouseOver.bind(this)
@@ -85,17 +63,30 @@ class Zoom extends Component {
8563
zoom,
8664
} = this.state
8765

88-
const {
89-
zoomScale,
90-
} = this.props
66+
const { zoomScale, height, width, img, transitionTime } = this.props
9167

9268
const transform = {
9369
transformOrigin: `${mouseX}% ${mouseY}%`,
9470
}
9571

72+
const outerDivStyle = {
73+
height: `${height}px`,
74+
width: `${width}px`,
75+
overflow: "hidden",
76+
};
77+
78+
const innerDivStyle = {
79+
height: `${height}px`,
80+
backgroundRepeat: "no-repeat",
81+
backgroundPosition: "center",
82+
backgroundSize: "auto 100%",
83+
transition: `transform ${transitionTime}s ease-out`,
84+
backgroundImage: `url('${img}')`,
85+
};
86+
9687
return (
9788
<div
98-
style={this.outerDivStyle}
89+
style={outerDivStyle}
9990
onMouseOver={this.handleMouseOver}
10091
onMouseOut={this.handleMouseOut}
10192
onMouseMove={this.handleMouseMovement}

0 commit comments

Comments
 (0)