Skip to content

useClosureListener and useClosureFilter#3

Open
deanrad wants to merge 1 commit intomainfrom
react-use-closure-handler
Open

useClosureListener and useClosureFilter#3
deanrad wants to merge 1 commit intomainfrom
react-use-closure-handler

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
@deanrad deanrad force-pushed the react-use-closure-handler branch from cef9dbf to ebf7ee2 Compare November 17, 2022 02:26
@deanrad deanrad force-pushed the main branch 3 times, most recently from 1fc5156 to f57c98f Compare January 4, 2024 20:33
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