Skip to content

Making changes to data prop incorrectly updates the cache #3032

Open
@akteigland

Description

@akteigland

Bug report

Description / Observed Behavior

I made changes to data returned from useSWR, to format the data. This updated the SWR cache with the formatted data, which later resulted in a different useSWR hook returning the formatted data, which is incorrect.

Expected Behavior

I would expect that any changes made to the data object be made locally only and not propagated to the SWR cache.

Repro Steps / Code Example

CodeSandbox

Additional Context

I don't think making changes directly to data is great practice anyway, but it can be fairly easy to do so accidentally. In my case I had mistakenly expected a lodash function to return a copy of the data and not the data object itself. While I understand why this would make changes to data locally, I was surprised to find this affected the cache as I would not expect changes outside of the fetcher function to make changes to the cache.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions