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

Add default values #197

Closed
franky47 opened this issue Feb 26, 2021 · 6 comments
Closed

Add default values #197

franky47 opened this issue Feb 26, 2021 · 6 comments
Assignees
Labels
feature New feature or request

Comments

@franky47
Copy link
Member

franky47 commented Feb 26, 2021

For when the query is not present, instead of returning null.

This should be done in a non-breaking way, as there are multiple use-cases for useQueryState:

  • Where it makes sense to have an empty/nullish value (eg: filtering)
  • Where it does not (eg: linkable controls)

Having a default value in React does not automatically update the URL to reflect it, which is debatable. If updating the URL, it would cause a side-effect depending on when the hook's parent component is mounted. If not, it breaks the idea that the URL is the source of truth (what if two hooks have different default values ?).

Use-cases in client code:

@franky47 franky47 added the feature New feature or request label Feb 26, 2021
@franky47
Copy link
Member Author

franky47 commented Mar 1, 2021

It's possible to use function overloads to detect the use of a default value in the options, and update the return type accordingly.

Before:
CleanShot 2021-03-01 at 14 02 37@2x

After:
CleanShot 2021-03-01 at 14 02 59@2x

Regarding two hooks with the same key having different default values, this fall out of the scope of this library. If someone wants to prevent such errors from happening, wrapping useQueryState into a custom hook could help keeping things DRY and in sync.

The URL will not be updated by the use of a default value if the query is missing. It's still possible to do setValue(null) to clear the query in the URL, and have the default value returned in the state.

franky47 added a commit that referenced this issue Mar 9, 2021
@franky47 franky47 self-assigned this Mar 15, 2021
@dbrxnds
Copy link

dbrxnds commented Sep 29, 2021

@franky47 Is there a reason as to why this hasn't been merged & released?

@franky47
Copy link
Member Author

@franky47 Is there a reason as to why this hasn't been merged & released?

Yes: I don't have time to work on this currently.

@dbrxnds
Copy link

dbrxnds commented Sep 29, 2021

Yes: I don't have time to work on this currently.

Ah, I thought your commit for this issue finished it.

@franky47
Copy link
Member Author

I think there are some caveats that may need attention before providing such a solution, as mentioned in the comments up there, like how to deal with URL/state discrepancies when using default values.

If you have some feedback on that matter, I'd love to hear it!

@franky47
Copy link
Member Author

franky47 commented Feb 2, 2022

Note: this was released in 1.4.0.

As for the behaviour with default values:

  • If there is no query string present, the state will be set to the default value
  • If there is a query string but it fails to parse, whatever the parser returns will be the state value (eg NaN for integer or float), the default value will not be returned there.
  • When setting the state to null, the query key will be removed from the URL and the default value will be returned as the state value.
  • The URL will never contain the default value (unless you manually set the state to it).

TL;DR: the default value only exists in the React tree as a fallback for when the query key is not present in the URL.

@franky47 franky47 closed this as completed Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants