Lines of Code | 46 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | import { PureComponent } from 'react'; |
||
2 | import { DEFAULT_SLICE } from '../constants'; |
||
3 | |||
4 | class BaseRouterComponent extends PureComponent { |
||
5 | |||
6 | constructor(props, context) { |
||
7 | super(props, context); |
||
8 | |||
9 | const { store, router } = context; |
||
10 | this.store = store; |
||
11 | this.router = router; |
||
12 | |||
13 | this.handleStoreChange = this.handleStoreChange.bind(this); |
||
14 | |||
15 | this.unsubscribe = store && store.subscribe(this.handleStoreChange); |
||
16 | } |
||
17 | |||
18 | componentDidMount() { |
||
19 | return this.handleStoreChange(); |
||
20 | } |
||
21 | |||
22 | componentWillUnmount() { |
||
23 | |||
24 | if (this.isSubscribed) { |
||
25 | this.unsubscribe(); |
||
26 | delete this.unsubscribe; |
||
27 | } |
||
28 | } |
||
29 | |||
30 | get isSubscribed() { |
||
31 | |||
32 | return typeof this.unsubscribe === 'function'; |
||
33 | } |
||
34 | |||
35 | handleStoreChange() { // eslint-disable-line class-methods-use-this |
||
36 | throw new Error('this.handleStoreChange() should be implemented'); |
||
37 | } |
||
38 | |||
39 | getStatefromStore() { |
||
40 | const { slice = DEFAULT_SLICE, immutable } = this.router; |
||
41 | const state = this.store.getState(); |
||
42 | return immutable ? state.get(slice) : state[slice]; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | export default BaseRouterComponent; |
||
47 |