Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ export function usePreferredLanguage() {
);
}

export function usePrevious(value) {
export function usePrevious(value, initialPreviousValue) {
const [current, setCurrent] = React.useState(value);
const [previous, setPrevious] = React.useState(null);

Expand All @@ -1023,7 +1023,7 @@ export function usePrevious(value) {
setCurrent(value);
}

return previous;
return previous || initialPreviousValue;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works as default value, not the initial. Imagine calling usePrevious<number>(value, 1) with the value being: 1, 2, 0, 3. And we'll see 1, 1, 2, 1, right? It would be nice to have some tests confirming it works for some inputs...

}

export function useQueue(initialValue = []) {
Expand Down
1 change: 1 addition & 0 deletions usehooks.com/src/content/hooks/usePrevious.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
| Name | Type | Description |
| -------- | ---- | -------------------------------------------------- |
| newValue | any | The new value to track and return the previous of. |
| initialValue | any | (Optional) The initial Previous Value. Default value is null.
</div>

### Return Value
Expand Down