@@ -205,15 +205,19 @@ class Switch extends Component {
205
205
}
206
206
207
207
componentWillMount ( ) {
208
- this . setState ( { checked : this . props . checked } ) ;
209
- this . _layoutThumb ( this . props ) ;
208
+ const nextState = this . _layoutThumb ( this . props . checked ,
209
+ this . props . thumbRadius ,
210
+ this . props . trackLength ,
211
+ this . props . trackSize ) ;
212
+ this . setState ( Object . assign ( nextState , { checked : this . props . checked } ) ) ;
210
213
}
211
214
212
215
componentWillReceiveProps ( nextProps ) {
213
- if ( nextProps . checked !== this . props . checked ) {
214
- this . setState ( { checked : nextProps . checked } ) ;
215
- }
216
- this . _layoutThumb ( nextProps ) ;
216
+ const nextState = this . _layoutThumb ( nextProps . checked ,
217
+ nextProps . thumbRadius ,
218
+ nextProps . trackLength ,
219
+ nextProps . trackSize ) ;
220
+ this . setState ( Object . assign ( nextState , { checked : nextProps . checked } ) ) ;
217
221
}
218
222
219
223
// Un-boxing the `Thumb` node from `AnimatedComponent`,
@@ -249,14 +253,15 @@ class Switch extends Component {
249
253
// property initializers end
250
254
251
255
// Layout the thumb according to the size of the track
252
- _layoutThumb ( { trackLength, trackSize } ) {
256
+ _layoutThumb ( checked , thumbRadius , trackLength , trackSize ) {
253
257
const trackRadii = trackSize / 2 ;
254
- const thumbRadii = this . props . thumbRadius ;
258
+ const thumbRadii = thumbRadius ;
255
259
const rippleRadii = trackLength - trackSize ;
256
260
const trackMargin = rippleRadii - trackRadii ; // make room for ripple
257
- const thumbLeft = this . state . checked ? trackMargin + trackRadii : 0 ;
261
+ const thumbLeft = checked ? trackMargin + trackRadii : 0 ;
258
262
this . _animatedThumbLeft . setValue ( thumbLeft ) ;
259
- this . setState ( {
263
+
264
+ return {
260
265
trackSize,
261
266
trackLength,
262
267
trackRadii,
@@ -267,7 +272,7 @@ class Switch extends Component {
267
272
r : thumbRadii ,
268
273
padding : rippleRadii - thumbRadii ,
269
274
} ,
270
- } ) ;
275
+ } ;
271
276
}
272
277
273
278
// Move the thumb left or right according to the current state
0 commit comments