-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
Can i work on this feature ? |
Yep feel free to pick it up 👍 |
Could this be a possible fix ? previously Lines 69 to 79 in b819c25
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> |
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 Lines 270 to 276 in b819c25
here |
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.The text was updated successfully, but these errors were encountered: