
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
evently-react
Advanced tools

evently-react simplifies event management for React developers. π
Whether you're building a small app or a large-scale application, this package offers a clean and simplified design to manage robust and scalable event-driven architectures.
With its hooks-based API, evently-react enables seamless event handling while keeping your codebase clean and maintainable.
Say goodbye to prop drilling & global state chaos and hello to elegant, event-driven designs! π―
useSubscribe, useEvent, and useSubscribeState.EventBus class for other environments.Install the package using npm or yarn:
npm install evently-react
# OR
yarn add evently-react
Below is a quick example to get started with evently-react.
import React from 'react'
import { EventProvider, useEvent, useSubscribe } from 'evently-react'
// Define a publisher component
const Publisher: React.FC = () => {
const { emitEvent } = useEvent()
const handleClick = () => {
emitEvent('greet', { message: 'Hello from Publisher!' }) // Emit an event
}
return <button onClick={handleClick}>Send Greeting</button>
}
// Define a subscriber component
const Subscriber: React.FC = () => {
useSubscribe('greet', (eventName, payload) => {
console.log(payload.message) // Log the event payload
})
return <p>Listening for greetings...</p>
}
// Wrap your application with EventProvider
const App: React.FC = () => (
<EventProvider>
<Publisher />
<Subscriber />
</EventProvider>
)
export default App
evently-react is designed to simplify communication in complex React applications. Here are some inspiring real-world use cases to help you explore its potential:
Emit an event in one component and listen to it in anotherβno need for prop drilling or context juggling.
Example:
// In Header component
emitEvent('toggleSidebar', { isVisible: true })
// In Sidebar component
useSubscribe('toggleSidebar', (eventName, payload) => setSidebarVisible(payload.isVisible))
Create a centralized system for displaying notifications (like toast messages) triggered from anywhere in your app.
Example:
emitEvent('showToast', { type: 'error', message: 'Failed to fetch data!' })
Trigger UI updates in real-time when backend events or user actions occur.
Example:
// WebSocket listener emits new data
emitEvent('updateMetrics', newMetrics)
// Dashboard listens to updates
useSubscribe('updateMetrics', (eventName, metrics) => updateChart(metrics))
Combine event-driven architecture with existing state management tools (e.g., Redux or Context API) for specific, modular use cases.
Example:
// Emit form reset event
emitEvent('resetForm')
// Form component subscribes to reset action
useSubscribe('resetForm', (eventName, payload) => setFormData(initialValues))
By adopting evently-react, you can create scalable, decoupled, and maintainable solutions for modern web applications. π―
Use the useEvent hook to emit events:
import React from 'react'
import { useEvent } from 'evently-react'
const EmitExample: React.FC = () => {
const { emitEvent } = useEvent()
const handleClick = () => {
emitEvent('exampleEvent', { message: 'Hello, Evently!' }) // Emit an event with a payload
}
return <button onClick={handleClick}>Emit Event</button>
}
export default EmitExample
Use the useSubscribe hook to listen to events:
import React from 'react'
import { useSubscribe } from 'evently-react'
const SubscribeExample: React.FC = () => {
useSubscribe('exampleEvent', (eventName, payload) => {
console.log('Received payload:', payload) // Logs: { message: 'Hello, Evently!' }
})
return <p>Listening for events...</p>
}
export default SubscribeExample
Use the enhanced useSubscribe hook to listen to multiple events:
import React from 'react'
import { useSubscribe } from 'evently-react'
const MultiSubscribeExample: React.FC = () => {
useSubscribe(['eventOne', 'eventTwo'], (eventName, payload) => {
console.log(`Received payload for ${eventName}:`, payload)
})
return <p>Listening for multiple events...</p>
}
export default MultiSubscribeExample
Leverage the useSubscribeState hook to access the latest event payload as state:
import React from 'react'
import { useSubscribeState } from 'evently-react'
const StateExample: React.FC = () => {
const latestEventPayload = useSubscribeState('exampleEvent')
return <p>Latest Payload: {latestEventPayload?.message}</p>
}
export default StateExample
Middleware allows you to intercept or transform events before they are processed.
.use()import { useEvent } from 'evently-react'
const App = () => {
const { eventBus } = useEvent()
eventBus.use((event, payload) => {
console.log(`Global Middleware: ${event}`)
return { ...payload, eventSeen: true }
})
return <div>Your App</div>
}
.useForEvent()eventBus.useForEvent('myEvent', (event, payload) => {
console.log(`Event middleware for ${event}`)
return { ...payload, transformed: true }
})
Late subscribers can still receive the latest events:
useSubscribe('exampleEvent', (eventName, payload) => {
console.log('Late subscriber received:', payload) // Works even if the event was emitted earlier
})
Control the execution order of event handlers by assigning priorities. Higher priority callbacks execute first (default is 0).
// Low priority handler.
useSubscribe('important-event', (eventName, payload) => console.log('Low priority'), 1)
// High priority handler.
useSubscribe('important-event', (eventName, payload) => console.log('High priority'), 3)
Handlers are executed in descending order of priority, making it easy to manage complex event flows.
The eventBus instance, accessible via the useEvent hook, allows integration of evently-react into non-Component environments such as utility functions or external libraries. This enables advanced use cases like chaining events, transforming payloads, or other customized workflows.
The following example demonstrates how to pass the eventBus instance to a utility function for chaining events:
// Component file
import { useEvent } from 'evently-react'
import { handleEventChain } from './utils'
const Component = () => {
const { eventBus } = useEvent()
// Pass the eventBus instance to utility functions
handleEventChain('logoutUser', eventBus)
return <div>Event Handling Component</div>
}
// utils.ts
import { EventBus } from 'evently-react'
export function handleEventChain(eventName: string, eventBus: EventBus) {
eventBus.subscribe(eventName, payload => {
if (payload.action === 'logout') {
console.log('Logging out user...')
// Emitting new event based on received payload
eventBus.emit('resetPreferences', { message: 'Resetting preferences...' })
}
})
}
Tip: If the use case is completely non-React, consider using the EventBus class directly. Instead of using hooks, you can create an instance of EventBus and leverage its methods for event management.
This feature extends the power of evently-react beyond React components, enabling seamless integration in diverse workflows. π
| Feature | Redux Equivalent | evently-react Alternative |
|---|---|---|
| Dispatch actions | dispatch | emitEvent |
| Select state | useSelector | useSubscribeState |
| Middleware/Event Processing | thunks/sagas | Global & Event-specific Middleware |
| Centralized store & reducer boilerplate | Required | Not Required |
| Prop drilling | Problematic | Eliminated |
Inject evently-react seamlessly into existing projects and reduce boilerplate while maintaining scalability.
Note: evently-react is not intended to replace Redux/Context API or prop drilling entirely. Instead, it provides a complementary event-driven approach to simplify code and improve scalability. By leveraging this pattern, you can reduce boilerplate and decouple components without sacrificing existing architecture.
evently-react with React context or Redux for hybrid state/event management solutions.evently-react?evently.d.ts file in your project and extend the Events type:import 'evently-react'
declare module 'evently-react' {
export interface Events {
showToast: { message: string; duration: number }
setTheme: { theme: 'light' | 'dark' }
}
}
evently-react in non-React environments?EventBus class instance to integrate event-based communication in non-React environments. (Example: Vanilla JS, Angular and other frameworks) All methods like emit, subscribe, and middleware support are fully available.useSubscribe and give array of event names as parameter. It will listen to all the events provided in the array.Having issues or have a suggestion?
FAQs
A Hook-Based Framework for Event-Driven React Apps
The npm package evently-react receives a total of 4 weekly downloads. As such, evently-react popularity was classified as not popular.
We found that evently-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.