Skip to content

Commit 74b5fbe

Browse files
Added Typescript version of usePrevious hook (#101)
* Added Typescript version of usePrevious hook * Simple type fix * Convert added typescript version of useWindowSize Co-authored-by: Nika Sepiskveradze <[email protected]>
1 parent 776247a commit 74b5fbe

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/pages/usePrevious.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,41 @@ function usePrevious(value) {
4545
return ref.current;
4646
}
4747
```
48+
49+
```typescript
50+
import { useState, useEffect, useRef } from "react";
51+
52+
// Usage
53+
function App() {
54+
// State value and setter for our example
55+
const [count, setCount] = useState<number>(0);
56+
57+
// Get the previous value (was passed into hook on last render)
58+
const prevCount: number = usePrevious<number>(count);
59+
60+
// Display both current and previous count value
61+
return (
62+
<div>
63+
<h1>
64+
Now: {count}, before: {prevCount}
65+
</h1>
66+
<button onClick={() => setCount(count + 1)}>Increment</button>
67+
</div>
68+
);
69+
}
70+
71+
// Hook
72+
function usePrevious<T>(value: T): T {
73+
// The ref object is a generic container whose current property is mutable ...
74+
// ... and can hold any value, similar to an instance property on a class
75+
const ref: any = useRef<T>();
76+
77+
// Store current value in ref
78+
useEffect(() => {
79+
ref.current = value;
80+
}, [value]); // Only re-run if value changes
81+
82+
// Return previous value (happens before update in useEffect above)
83+
return ref.current;
84+
}
85+
```

src/pages/useWindowSize.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,56 @@ function useWindowSize() {
5656
return windowSize;
5757
}
5858
```
59+
60+
```typescript jsx
61+
import { useState, useEffect } from "react";
62+
63+
// Define general type for useWindowSize hook, which includes width and height
64+
interface Size {
65+
width: number | undefined,
66+
height: number | undefined,
67+
}
68+
69+
// Usage
70+
function App() {
71+
const size: Size = useWindowSize();
72+
73+
return (
74+
<div>
75+
{size.width}px / {size.height}px
76+
</div>
77+
);
78+
}
79+
80+
// Hook
81+
function useWindowSize(): Size {
82+
// Initialize state with undefined width/height so server and client renders match
83+
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
84+
const [windowSize, setWindowSize] = useState<Size>({
85+
width: undefined,
86+
height: undefined,
87+
});
88+
89+
useEffect(() => {
90+
// Handler to call on window resize
91+
function handleResize() {
92+
// Set window width/height to state
93+
setWindowSize({
94+
width: window.innerWidth,
95+
height: window.innerHeight,
96+
});
97+
}
98+
99+
// Add event listener
100+
window.addEventListener("resize", handleResize);
101+
102+
// Call handler right away so state gets updated with initial window size
103+
handleResize();
104+
105+
// Remove event listener on cleanup
106+
return () => window.removeEventListener("resize", handleResize);
107+
}, []); // Empty array ensures that effect is only run on mount
108+
109+
return windowSize;
110+
}
111+
```

0 commit comments

Comments
 (0)