Skip to content

Commit bbac2c3

Browse files
committed
Fix setting idx
1 parent 6e75c2c commit bbac2c3

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/AppHistory.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function useAppHistory(): object{
55
const location: Location = useLocation();
66
const navigationType: NavigationType = useNavigationType();
77

8-
const [idx, setIdx] = useState(-1);
8+
const idx: RefObject<number> = useRef<number>(-1);
99

1010
const [index, setIndex] = useState(-1);
1111
const stack: RefObject<object[]> = useRef<object[]>([location]);
@@ -14,20 +14,21 @@ function useAppHistory(): object{
1414
if(navigationType===NavigationType.Push){
1515
stack.current.splice(index+1);
1616
stack.current.push(location);
17-
setIdx(history.state.idx);
17+
idx.current = history.state.idx;
1818
setIndex(index+1);
1919
}
2020
if(navigationType===NavigationType.Pop){
21-
if(idx>history.state.idx){
21+
if(idx.current>history.state.idx){
2222
setIndex(index-1);
2323
}
24-
if(idx<history.state.idx){
24+
if(idx.current<history.state.idx){
2525
setIndex(index+1);
2626
}
27-
setIdx(history.state.idx);
27+
idx.current = history.state.idx;
2828
}
2929
if(navigationType===NavigationType.Replace){
3030
stack.current[index] = location;
31+
idx.current = history.state.idx;
3132
}
3233
},[location,navigationType]);
3334

0 commit comments

Comments
 (0)