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

useClosureListener and useClosureFilter #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

deanrad
Copy link
Owner

@deanrad deanrad commented Nov 17, 2022

When an event listener (or filter) needs to always have the latest of certain React variables,
useClosureListener and useClosureFilter accept:

  • A criteria
  • A handler (a 'bound' function closing over some values)
  • An observer (if a listener - does not apply to filters)
  • A deps array - any change in one of these will replace the handler with the one given (per useEffect)

One good application for useClosureFilter is to create a component whose job is to listen to certain events, and add stuff to their payload - stuff that depends on React current values.

// Adds to the payload of FILE_OPEN events the CURRENT_USER.userId
const FILE_OPEN = createEvent('file/open');
const { data, loading } = useQuery(CURRENT_USER);
useClosureFilter(
  FILE_OPEN,
  (e) => { e.payload.userId = data?.userId; return e; }, 
  [data]
);

@deanrad deanrad force-pushed the react-use-closure-handler branch 2 times, most recently from bdc4730 to cef9dbf Compare November 17, 2022 02:21
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.

1 participant