Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default value for usePrevious hook #275

Open
ImanMahmoudinasab opened this issue Jan 25, 2024 · 0 comments
Open

default value for usePrevious hook #275

ImanMahmoudinasab opened this issue Jan 25, 2024 · 0 comments

Comments

@ImanMahmoudinasab
Copy link

The current behavior of the usePrevious hook is to always return null as the previous value. But in some cases, it makes sense to have a different default value. For example for a table's data we don't want to pass null as the value instead we want to pass an empty array:

function MyTable({data, loading}){

const previousData = usePrevious(data);

 // This throws an exception when the component gets rendered with loading true:
const table = useReactTable({data: loading ? previousData: data});

return (
     // ...
);
}

Instead, I would like to be able to set an initial value for the hook:

function MyTable({data, loading}){

const previousData = usePrevious(data, []);

 // No error this time since previousData is [] at first render when loading is true:
const table = useReactTable({data: loading ? previousData: data});

return (
     // ...
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant