The React Native Event Manager is a utility library designed to simplify event handling and management in React Native applications. It provides a centralized event management system that allows components to communicate efficiently through events.
npm install react-native-event-manager
The ComponentA checks for changes in the 'onUpdate' state
import { useOnEvent } from 'react-native-event-manager'
function ComponentA(): JSX.Element {
const [value, setValue] = useState(-1)
useOnEvent('onUpdate', (newValue) => {
setValue(newValue)
})
return(
<Text>{value}</Text>
)
}
The ComponentB notify a change to the 'onUpdate' state
import { notifyEvent } from 'react-native-event-manager'
function ComponentB(): JSX.Element {
const [count, setCount] = useState(0)
const handlePress = () => {
notifyEvent('onUpdate', count)
setCount(count + 1)
}
return(
<Button title='Send' onPress={handlePress}/>
)
}
constructor()
Initializes the EventManager instance.
get()
Retrieves the singleton instance of the EventManager.
getListener(index: number): EventListener | undefined
Retrieves a listener at the specified index.
getListenerCount(): number
Returns the number of registered listeners.
removeAllListener(eventName: string | null): boolean
Removes all listeners for the specified event name. If eventName is null, removes all listeners.
remove(listener: EventListener): boolean
Removes the specified listener from the event manager.
sync(eventName: string): Promise<V | null>
Returns a Promise that resolves with the value of the next occurrence of the specified event.
on(eventName: string, callback: Callback): EventListener
Subscribes a listener to the specified event. Returns the created EventListener.
once(eventName: string, callback: Callback): EventListener
Subscribes a listener to the specified event, automatically removing it after the first occurrence. Returns the created EventListener.
notify(eventName: string, value?: V): void
Notifies all listeners of the specified event with an optional value.
constructor(eventManager: EventManager, name: string, callback: Callback)
Initializes an EventListener instance with the specified event manager, event name, and callback function.
remove()
Removes the listener from the event manager.
update(value?: T)
Notifies all registered callbacks with an optional value.
useOnEvent(eventName: string, callback: Callback)
A React hook for subscribing to a specific event. Automatically removes the listener on component unmount.
useOnEvents(observers: { [key: string]: Callback; })
A React hook for subscribing to multiple events specified in the observers object. Automatically removes all listeners on component unmount.
notifyEvent(eventName: string, value?: V): void
Notifies all listeners of the specified event with an optional value.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library