Skip to content

Commit 50ea54c

Browse files
committed
Store scroll behavior as instance property instead of state
1 parent e128ac5 commit 50ea54c

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

modules/mixins/ScrollContext.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,57 @@ var ScrollContext = {
5858
},
5959

6060
componentWillMount: function () {
61-
var behavior = this.getScrollBehavior();
62-
6361
invariant(
64-
behavior == null || canUseDOM,
62+
this.getScrollBehavior() == null || canUseDOM,
6563
'Cannot use scroll behavior without a DOM'
6664
);
67-
68-
if (behavior != null)
69-
this._scrollPositions = {};
7065
},
7166

7267
recordScroll: function (path) {
73-
if (this._scrollPositions)
74-
this._scrollPositions[path] = getWindowScrollPosition();
68+
var positions = this.getScrollPositions();
69+
positions[path] = getWindowScrollPosition();
7570
},
7671

7772
updateScroll: function (path, actionType) {
78-
if (this._scrollPositions) {
79-
var behavior = this.getScrollBehavior();
80-
var position = this._scrollPositions[path];
73+
var behavior = this.getScrollBehavior();
74+
var position = this.getScrollPosition(path);
8175

82-
if (behavior && position)
83-
behavior.updateScrollPosition(position, actionType);
84-
}
76+
if (behavior && position)
77+
behavior.updateScrollPosition(position, actionType);
8578
},
8679

8780
/**
8881
* Returns the scroll behavior object this component uses.
8982
*/
9083
getScrollBehavior: function () {
91-
return this.state.scrollBehavior;
84+
if (this._scrollBehavior == null) {
85+
var behavior = this.props.scrollBehavior;
86+
87+
if (typeof behavior === 'string')
88+
behavior = NAMED_SCROLL_BEHAVIORS[behavior];
89+
90+
this._scrollBehavior = behavior;
91+
}
92+
93+
return this._scrollBehavior;
94+
},
95+
96+
/**
97+
* Returns a hash of URL paths to their last known scroll positions.
98+
*/
99+
getScrollPositions: function () {
100+
if (this._scrollPositions == null)
101+
this._scrollPositions = {};
102+
103+
return this._scrollPositions;
104+
},
105+
106+
/**
107+
* Returns the last known scroll position for the given URL path.
108+
*/
109+
getScrollPosition: function (path) {
110+
var positions = this.getScrollPositions();
111+
return positions[path];
92112
},
93113

94114
childContextTypes: {

0 commit comments

Comments
 (0)