1
- import React from 'react' ;
1
+ import { Component , PropTypes } from 'react' ;
2
2
import ScrollBehavior from 'scroll-behavior/lib/ScrollBehavior' ;
3
3
4
- export default class ScrollBehaviorContainer extends React . Component {
4
+ export default class ScrollBehaviorContainer extends Component {
5
5
static propTypes = {
6
- shouldUpdateScroll : React . PropTypes . func ,
7
- routerProps : React . PropTypes . object . isRequired ,
8
- children : React . PropTypes . node . isRequired ,
6
+ shouldUpdateScroll : PropTypes . func ,
7
+ routerProps : PropTypes . object . isRequired ,
8
+ children : PropTypes . node . isRequired ,
9
9
} ;
10
10
11
- componentDidMount ( ) {
11
+ static childContextTypes = {
12
+ scrollBehavior : PropTypes . instanceOf ( ScrollBehavior ) ,
13
+ } ;
14
+
15
+ constructor ( ) {
16
+ super ( ) ;
17
+
18
+ this . state = {
19
+ scrollBehavior : null ,
20
+ } ;
21
+ }
22
+
23
+ getChildContext ( ) {
24
+ return {
25
+ scrollBehavior : this . scrollBehavior ,
26
+ } ;
27
+ }
28
+
29
+ componentWillMount ( ) {
12
30
const { routerProps } = this . props ;
13
31
14
32
this . scrollBehavior = new ScrollBehavior (
15
33
routerProps . router ,
16
34
( ) => this . props . routerProps . location
17
35
) ;
18
36
37
+ // Set state so child context will be updated
38
+ this . setState ( { scrollBehavior : this . scrollBehavior } ) ;
39
+
19
40
this . onUpdate ( null , routerProps ) ;
20
41
}
21
42
@@ -37,16 +58,18 @@ export default class ScrollBehaviorContainer extends React.Component {
37
58
onUpdate ( prevRouterProps , routerProps ) {
38
59
const { shouldUpdateScroll } = this . props ;
39
60
40
- let scrollPosition ;
41
- if ( ! shouldUpdateScroll ) {
42
- scrollPosition = true ;
43
- } else {
44
- scrollPosition = shouldUpdateScroll . call (
45
- this . scrollBehavior , prevRouterProps , routerProps
46
- ) ;
47
- }
61
+ this . scrollBehavior . getContainerKeys ( ) . forEach ( containerKey => {
62
+ let scrollPosition ;
63
+ if ( ! shouldUpdateScroll ) {
64
+ scrollPosition = true ;
65
+ } else {
66
+ scrollPosition = shouldUpdateScroll . call (
67
+ this . scrollBehavior , prevRouterProps , routerProps , containerKey
68
+ ) ;
69
+ }
48
70
49
- this . scrollBehavior . updateScroll ( scrollPosition ) ;
71
+ this . scrollBehavior . updateScroll ( containerKey , scrollPosition ) ;
72
+ } ) ;
50
73
}
51
74
52
75
render ( ) {
0 commit comments