Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

<RecoilURLSync> doesn't reset state if param is not present #1900

Closed
@koichik

Description

@koichik

RecoilURLSync{JSON,Transit} can take a param if the location's part is 'queryParams'.
However, if the new URL doesn't have the part, RecoilURLSync{JSON,Transit} doesn't reset a state that syncing it.

for example,

Atom:

const nameState = atom<string>({
  key: 'name',
  default: '',
  effects: [syncEffect({ refine: string() })],
})

App:

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilURLSyncJSON
        location={{ part: 'queryParams', param: 'state' }}
      >
        <Component {...pageProps} />
      </RecoilURLSyncJSON>
    </RecoilRoot>
  )
}
  1. access to:
    http://localhost:3000/users/1

  2. change name state to 'Foo', then URL is changed to:
    http://localhost:3000/users/1?state=%7B%22name%22%3A%22Foo%22%7D

  3. navigate to:
    http://localhost:3000/users/2

expected: the name state is reset to the default (empty string).
actual: the name state still 'Foo'.

I think that this line should return empty Map() instead of null when stateStr is null

return stateStr != null ? wrapState(deserialize(stateStr)) : null;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions