Skip to content
This repository has been archived by the owner on Dec 3, 2022. It is now read-only.

useFocusEffect / useIsFocused #43

Merged
merged 8 commits into from
Oct 2, 2019
Merged

useFocusEffect / useIsFocused #43

merged 8 commits into from
Oct 2, 2019

Conversation

slorber
Copy link
Member

@slorber slorber commented Sep 25, 2019

@satya164 does it feel ok?

Didn't use the use-subscription package for now, didn't know about it but will take a look.

Also didn't invest in tests because there's not yet any test infra, not much time, and this project is probably only temporary until people use the v5. But those hooks will be used in my app right now so I'm pretty confident to see if something breaks.

Will add some doc too.

src/Hooks.ts Outdated Show resolved Hide resolved
src/Hooks.ts Outdated Show resolved Hide resolved
src/Hooks.ts Outdated Show resolved Hide resolved
src/Hooks.ts Outdated Show resolved Hide resolved
src/Hooks.ts Show resolved Hide resolved
@slorber slorber changed the title useFocusEffect / useIsFocused / useBackHandler useFocusEffect / useIsFocused Sep 29, 2019
@slorber
Copy link
Member Author

slorber commented Sep 29, 2019

Hi @satya164 I've made some changes and added some doc

README.md Outdated Show resolved Hide resolved
src/Hooks.ts Outdated Show resolved Hide resolved
slorber and others added 2 commits September 30, 2019 13:23
Co-Authored-By: Satyajit Sahoo <satyajit.happy@gmail.com>
Co-Authored-By: Satyajit Sahoo <satyajit.happy@gmail.com>
@slorber
Copy link
Member Author

slorber commented Sep 30, 2019

Thanks :) seems ready to merge now

@slorber slorber merged commit 2a5b198 into master Oct 2, 2019
focusSubscription.remove();
blurSubscription.remove();
};
}, [callback, navigation]);

Choose a reason for hiding this comment

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

@slorber If I run setParams in callback, navigation will change and invoke callback again, it will raise a infinite loop

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for reporting this.

@satya164 how do you handle those loops in v5? I guess we should fix it the same way in v5 and here

Copy link
Member

Choose a reason for hiding this comment

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

v5 doesn't update navigation prop on params update since route is a separate prop. regarding how to handle it in v4, user needs to add a check manually that params are out of date before trying to update them. they should do it anyways to prevent unnecessary updates to the screen regardless of the loop anyways since we don't do any equality checks like setState does.

Copy link
Member Author

Choose a reason for hiding this comment

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

@satya164 I understand your point about preventing useless re-renders/infinite loops.

But if param value has to change, we still don't necessarily want to re-execute useFocusEffect again (at least user should be in control imho)

I mean:

This one should re-execute on userId param change

useFocusEffect(useCallback(() => { ... },[params.userId]));

But this one should rather not re-execute even if any param changes:

useFocusEffect(useCallback(() => { ... },[]));

So I think it's not correct to make this hook depend on navigation here, as the object is unstable it will tend to produce unwanted effect re-execution. I guess it will even re-execute if the parent re-renders for example.

I've opened another issue: #48

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

Successfully merging this pull request may close these issues.

None yet

3 participants