Skip to content

Micro optimizations #1018

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

Merged
merged 8 commits into from
Mar 13, 2021
Merged

Micro optimizations #1018

merged 8 commits into from
Mar 13, 2021

Conversation

shuding
Copy link
Member

@shuding shuding commented Mar 7, 2021

Make the code cleaner and more consistent:

  • use the useState({})[1] pattern for re-rendering the component
  • make the internal property names shorter in Cache
  • make the key prefixes in useSWRInfinite 3 chars (ctx@, inf@, len@), and internal cache property name shorter
  • make addRevalidator return the ubsubscriber

Closes #896.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 7, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 972e4e7:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration

@shuding shuding removed the request for review from Timer March 10, 2021 12:51
Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally lgtm. the lint warning is a fake one. disabling it or rollback to array destruction are both acceptable.
eslint hooks ignore the none array pattern for useState, haven't found any useful case to work around yet

@shuding
Copy link
Member Author

shuding commented Mar 13, 2021

@huozhi It seems like the linter didn't yell, so I think it's fine :)

This was referenced Mar 15, 2021
@ranisalt
Copy link

@shuding care to point out what is the useState({})[1] trick? I'm curious on what it changes.

@shuding
Copy link
Member Author

shuding commented Mar 16, 2021

@ranisalt it's an implementation detail inside SWR, previously we are using const [,rerender] = useState(false) to create a re-render function, now it uses const rerender = useState({})[1].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants