Git Product home page Git Product logo

jotai-location's People

Contributors

arjunvegda avatar dai-shi avatar v-mann-nick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jotai-location's Issues

[atomWithHash] When returning to the first screen with history.back, the atom value is not initialized.

Summary

If I go back to the first screen with no hash, hashchange event will not fire.
So, the atom value is not initialized.

I think we can use popstate instead of hashchange.
The popstate will fire even if the hash does not change, but I think it is okay because setAtom compares the previous data with the next data.

Please let me know if there is something I am not thinking of.

Link to reproduction

Here is the codesandbox.

Check List

Please do not ask questions in issues.

  • I understand this is not a question.

Please include a minimal reproduction.

`atomWithHash` initialValue from URL hash?

I'm making use of atomWithHash to allow one page to transmit "state" to another.

For example, my products page: /order/products might have an initial state of []

Consider this getting passed to a form page e.g./order/checkout, which needs to differentiate page content based on the products that were selected.

If I pass from products to checkout, everything is fine, as atomWithHash has been initialized already, and the user has gone through a predictable flow. The atom products is populated (my products array)

One case I can think of, is that if the user lands on the checkout page without having selected any products, I should redirect them to the product selection page (/order/products in this example). This behaviour should be true even if the user lands directly on the checkout page, it's an edge case the UI needs to account for. If you have not selected any products, I should redirect you to the selection page.

But if I load the page (even with a hash e.g. /order/checkout#products=MY_CORRECT_HASH) the initial value is [], it takes a re-render to populate the actual value - This means I cannot differentiate between the state between initialized value vs a user not having selected any products.

Any advice on how to approach this problem?

adding typesafety for atomWithLocation

Hey, thanks for the hard work you have been putting into this new component,
I was wondering if you ever thought about adding type safety to atomWithLocation and/or serialization
We currently use Jotai and Jotai-location on our production app (NextJS), and it's sometimes a bit confusing to remember all the query keys if it needs to be shared across multiple components.

We were wondering if it would be possible for the feature to have something like this:

type LocationQuery = {
  search?: string;
  take?: number;
  skip?: number;
}

export const locAtom = atomWithLocation<LocationQuery>({
  // seralize ?
  // deserialize ?
  // initalValue ?
});

It might be out of the scope of this library but it just makes the DX much better.
Thanks again!

P.S
some resources:
https://github.com/pbeshai/use-query-params
https://github.com/amannn/next-query-params

type AtomWithHashOptions

I'm learning AtomWithHashOptions. Does this type make sense? If so, please add it at the next opportunity.

export type AtomWithHashOptions<Value> = {
    /**
     * A function to serialize the value before storing it in the URL hash.
     * The serialized string will be decoded by the `deserialize` function.
     * Default: `JSON.stringify`
     */
    serialize?: (val: Value) => string;
    /**
     * A function to deserialize the value retrieved from the URL hash.
     * The deserialized value will be used as the initial value of the atom.
     * If the deserialized value is `NO_STORAGE_VALUE`, the atom will be initialized with the `initialValue`.
     * Default: `(str) => NO_STORAGE_VALUE`
     */
    deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE;
    /**
     * A boolean flag that indicates whether to use `window.history.replaceState` instead of `window.location.hash`.
     * When this flag is set to `true`, the URL hash will be replaced instead of being added to the browser history.
     * Note: This flag is deprecated in favor of using `setHash` with `"replaceState"`.
     * Default: `false`
     * @deprecated Use `setHash` with 'replaceState' instead
     */
    replaceState?: boolean;
    /**
     * A function that will be called when the URL hash changes.
     * The callback should update the atom value with the new hash value.
     * Default: `(callback) => { window.addEventListener("hashchange", callback); return () => window.removeEventListener("hashchange", callback); }`
     * @returns A function that can be called to unsubscribe from the changes.
     */
    subscribe?: (callback: () => void) => () => void;
    /**
     * A function to set the URL hash.
     * By default, it sets the hash using `window.location.hash`.
     * When `replaceState` is set to true, it uses `window.history.replaceState` instead.
     * If a function is provided, it will be used instead.
     * The searchParams argument is the stringified version of the atom value.
     * Default: `"default"`
     * Options:
     * - `"default"`: Sets the hash using `window.location.hash`.
     * - `"replaceState"`: Sets the hash using `window.history.replaceState`.
     * - `(searchParams: string) => void`: A custom function that will be used to set the hash.
     */
    setHash?: "default" | "replaceState" | ((searchParams: string) => void);
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.