/
hash-changes-with-state.js
64 lines (58 loc) · 1.64 KB
/
hash-changes-with-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { Component } from 'react'
import Router from 'next/router'
let count = 0
export default class SelfReload extends Component {
static getInitialProps({ res }) {
if (res) return { count: 0 }
count += 1
return { count }
}
handleAClick = () => {
Router.push(
'/nav/hash-changes-with-state',
'/nav/hash-changes-with-state#',
{
historyCount: (window.history.state.options.historyCount || 0) + 1,
shallowHistoryCount: window.history.state.options.shallowHistoryCount,
}
)
}
handleAShallowClick = () => {
Router.push(
'/nav/hash-changes-with-state#',
'/nav/hash-changes-with-state#',
{
shallow: true,
historyCount: window.history.state.options.historyCount,
shallowHistoryCount:
(window.history.state.options.shallowHistoryCount || 0) + 1,
}
)
}
render() {
return (
<div id="hash-changes-page">
<p>COUNT: {this.props.count}</p>
<a id="increment-history-count" onClick={this.handleAClick}>
Increment history count
</a>
<div id="history-count">
HISTORY COUNT:{' '}
{typeof window !== 'undefined' &&
window.history.state.options.historyCount}
</div>
<a
id="increment-shallow-history-count"
onClick={this.handleAShallowClick}
>
Increment shallow history count
</a>
<div id="shallow-history-count">
SHALLOW HISTORY COUNT:{' '}
{typeof window !== 'undefined' &&
window.history.state.options.shallowHistoryCount}
</div>
</div>
)
}
}