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

chore: enhance useTrackedEffect type #2196

Merged
merged 4 commits into from
Jun 6, 2023
Merged

chore: enhance useTrackedEffect type #2196

merged 4 commits into from
Jun 6, 2023

Conversation

xvxlb
Copy link
Contributor

@xvxlb xvxlb commented May 22, 2023

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

fix: #2195

💡 Background and solution

#2195

📝 Changelog

Language Changelog
🇺🇸 English chore: enhance useTrackedEffect type
🇨🇳 Chinese chore: useTrackedEffect 类型修改

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@CLAassistant
Copy link

CLAassistant commented May 22, 2023

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 3 committers have signed the CLA.

❌ wei.xue
❌ xvxlb
❌ crazylxr


wei.xue seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@liuyib
Copy link
Collaborator

liuyib commented May 22, 2023

I find a better way to do this:

import type { DependencyList } from 'react';
import { useEffect, useRef } from 'react';

- type Effect<T extends unknown[]> = (
+ type Effect<T extends DependencyList> = (
   changes?: number[],
-  previousDeps?: [...T],
+  previousDeps?: T,
-  currentDeps?: [...T],
+  currentDeps?: T,
) => void | (() => void);

const diffTwoDeps = (deps1?: DependencyList, deps2?: DependencyList) => {
  //Let's do a reference equality check on 2 dependency list.
  //If deps1 is defined, we iterate over deps1 and do comparison on each element with equivalent element from deps2
  //As this func is used only in this hook, we assume 2 deps always have same length.
  return deps1
    ? deps1
        .map((_ele, idx) => (!Object.is(deps1[idx], deps2?.[idx]) ? idx : -1))
        .filter((ele) => ele >= 0)
    : deps2
    ? deps2.map((_ele, idx) => idx)
    : [];
};

- const useTrackedEffect = <T extends unknown[]>(effect: Effect<T>, deps?: [...T]) => {
+ const useTrackedEffect = <T extends DependencyList>(effect: Effect<T>, deps?: [...T]) => {
  const previousDepsRef = useRef<T>();

  useEffect(() => {
    const changes = diffTwoDeps(previousDepsRef.current, deps);
    const previousDeps = previousDepsRef.current;
    previousDepsRef.current = deps;
    return effect(changes, previousDeps, deps);
  }, deps);
};

export default useTrackedEffect;

I think the built-in type DependencyList is better than unknown[], what do you think? @xvxlb

@xvxlb
Copy link
Contributor Author

xvxlb commented May 22, 2023

@liuyib This seems to be more accurate 👍

Copy link
Collaborator

@liuyib liuyib left a comment

Choose a reason for hiding this comment

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

Nice work, thx

@xvxlb
Copy link
Contributor Author

xvxlb commented Jun 5, 2023

@liuyib need merge.

@liuyib
Copy link
Collaborator

liuyib commented Jun 5, 2023

@liuyib need merge.

I don't have permission to merge :)

@crazylxr review, pls

@crazylxr crazylxr merged commit 7121d08 into alibaba:master Jun 6, 2023
7 checks passed
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.

The argments's type of useTrackedEffect's effect function can not be accurately deduced.
4 participants