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

Make returned state (data, error, isValidating) readonly #1386

Closed
shuding opened this issue Aug 27, 2021 · 5 comments · Fixed by #1431 or #1514
Closed

Make returned state (data, error, isValidating) readonly #1386

shuding opened this issue Aug 27, 2021 · 5 comments · Fixed by #1431 or #1514
Labels
feature request New feature or request good first issue Good for newcomers

Comments

@shuding
Copy link
Member

shuding commented Aug 27, 2021

We can use mark the properties of the SWR return state readonly, to avoid modifying them. For example it's dangerous to directly modify the data object, which could cause an infinite loop of rerender.

@shuding shuding added feature request New feature or request good first issue Good for newcomers labels Aug 27, 2021
@swarup4741
Copy link

Can i work on this feature ?

@shuding
Copy link
Member Author

shuding commented Aug 28, 2021

Yep feel free to pick it up 👍

@swarup4741
Copy link

swarup4741 commented Aug 31, 2021

Could this be a possible fix ?

previously

swr/src/types.ts

Lines 69 to 79 in b819c25

export type SWRHook = <Data = any, Error = any>(
...args:
| readonly [Key]
| readonly [Key, Fetcher<Data> | null]
| readonly [Key, SWRConfiguration<Data, Error> | undefined]
| readonly [
Key,
Fetcher<Data> | null,
SWRConfiguration<Data, Error> | undefined
]
) => SWRResponse<Data, Error>

After

export type SWRHook = <Data = Readonly<any>, Error = Readonly<any>>(
  ...args:
    | readonly [Key]
    | readonly [Key, Fetcher<Data> | null]
    | readonly [Key, SWRConfiguration<Data, Error> | undefined]
    | readonly [
        Key,
        Fetcher<Data> | null,
        SWRConfiguration<Data, Error> | undefined
      ]
) => SWRResponse<Data, Error>

@shuding
Copy link
Member Author

shuding commented Aug 31, 2021

I think this only marks the parameters as read only, not the return value.

@swarup4741
Copy link

swarup4741 commented Sep 1, 2021

I think this only marks the parameters as read only, not the return value.

Then i think we need to freeze the data and error object while returning them

return {
    mutate: boundMutate,
    get data() {
      stateDependencies.data = true
      return Object.freeze(data)
    },
    get error() {
      stateDependencies.error = true
      return Object.freeze(error)
    },
    get isValidating() {
      stateDependencies.isValidating = true
      return isValidating
    }
  } as SWRResponse<Data, Error>
}

Also i found another type error

swr/src/use-swr.ts

Lines 270 to 276 in b819c25

cache.set(keyErr, err)
if (stateRef.current.error !== err) {
// Keep the stale data but update error.
setState({
isValidating: false,
error: err
})

here err has type unknown .... i think it shall be of type Error | undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request good first issue Good for newcomers
Projects
None yet
3 participants