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

useDerivedState支持传入自定义props对比方法 #75

Closed
yuxuan opened this issue Mar 11, 2021 · 4 comments
Closed

useDerivedState支持传入自定义props对比方法 #75

yuxuan opened this issue Mar 11, 2021 · 4 comments

Comments

@yuxuan
Copy link

yuxuan commented Mar 11, 2021

目前的实现方式如下:

if (previousPropValue !== propValue) {

这样的话当是非primitive的值对比时,是一直不等的。

是否可以添加支持传入propCompare函数,支持用户自定义对比方式?

@otakustay
Copy link
Member

这个功能现在是用compute来实现的,可以这么写:

useDerivedState(
    propValue,
    (propValue, stateValue) => {
        return customEquals(propValue, stateValue) ? stateValue : propValue;
    }
);

@yuxuan
Copy link
Author

yuxuan commented Apr 10, 2021

emm, 但是当props为非primitive的值时,还是会一直触发这个compute不是,触发下面两次setState,但是实际上是想说props是没变的。不一定是默认值,默认值可以memo或者提到constant去来解决。

@otakustay
Copy link
Member

因为React在setState时值一样是会bail out的,所以下面的2次setState只有第2次实际生效。而第二次讲道理是不应该跳过的

我感觉你更需要useOriginalCopyuseDerivedState联合使用

@yuxuan
Copy link
Author

yuxuan commented Apr 10, 2021

了解

@yuxuan yuxuan closed this as completed Apr 10, 2021
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

No branches or pull requests

2 participants