![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
React hook to subscribe and dispatch events accros React components
import { dispatch } from 'use-bus'
:
dispatch('string')
: will dispatch the action { type: 'string' }
without payloaddispatch({ type: 'string', payload: 3 })
: will dispatch the given actionimport useBus from 'use-bus'
:
useBus('string', callback, deps)
: register the given callback
to the string
action (action.type equals string
)
callback
: take the action as the first argument so you can retrieve its type and its payload for exampledeps
: is an array where you declare variables you use in callback
, like you are doing for a useEffect from Reactimport React, { useState } from 'react'
import useBus from 'use-bus'
const PrintIterations = () => {
const [iterations, setIterations] = useState(0)
useBus(
'@@ui/ADD_ITERATION',
() => setIterations(iterations + 1),
[iterations],
)
return (
<div>
{'There is '}
{iterations}
{' iterations'}
</div>
)
}
export default PrintIterations
useBus
@@ui/ADD_ITERATION
import React from 'react'
import { dispatch } from 'use-bus'
const IterateBtn = () => {
return (
<button onClick={() => dispatch('@@ui/ADD_ITERATION')}>
Iterate
</button>
)
}
export default IterateBtn
dispatch
and call it with the event you want to sendimport React from 'react'
import PrintIterations from './printIterations'
import IterateBtn from './iterateBtn'
const App = () => {
return (
<div>
<PrintIterations />
<IterateBtn />
</div>
)
}
export default App
There is no connection to do, this is already done by use-bus
.
This example just demonstrate that siblings can interact, but you can imagine a dispatcher wherever you want in the React tree and something that react to the dispatch wherever you want to.
FAQs
> React hook to subscribe and dispatch events across React components
The npm package use-bus receives a total of 2,458 weekly downloads. As such, use-bus popularity was classified as popular.
We found that use-bus 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
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.