Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
treble-hook
Advanced tools
Super easy way to get "hooked" on subscribe-and-publish in React with no dependencies and no cruft.
yarn add @igneous.io.ux/treble-hook
npm install --save @igneous.io.ux/treble-hook
color-picker.tsx
import usePubSub from '@igneous.io.ux/treble-hook'
import React, { FC } from 'react'
import { BluePicker, GreenPicker, RedPicker } from './color-buttons'
const DEFAULT_COLOR_STATE = '[Pick a color by clicking a button]'
export default function ColorPicker() {
const [
color,
publishColor,
unsubscribeFromColor
] = usePubSub<string>('picked-color', DEFAULT_COLOR_STATE)
return (
<h3><span style={{ color: color }}>Selected color: { color }</span></h3>
<RedPicker /><BluePicker /><GreenPicker />
<br/>
<button onClick={ () => { publishColor(DEFAULT_COLOR_STATE) }}>
Reset
</button>
<button onClick={ () => { unsubscribeFromColor() }}>
Click to stop receiving color picks
</button>
)
}
color-buttons.tsx
import usePubSub from '@igneous.io.ux/treble-hook'
import React from 'react'
const ColorButton = <{ cn: string }>({ cn }) => {
const [_, publishColor] = usePubSub<string>('picked-color', '')
return (
<button
style={{ color: cn }}
onClick={ () => { publishColor(cn) }}
>
{ cn.toUpperCase() }
</button>
)
}
export const RedButton = () => <ColorButton cn='red' />
export const GreenButton = () => <ColorButton cn='green' />
export const BlueButton = () => <ColorButton cn='blue' />
The advent of Hooks in React 16.8 introduced a paradigm shift in how state can be managed in applications built on top of React. Arguably, the defacto standard for managing application state prior to 16.8 was Redux. This worked great but it came with its own idiosyncrasies and a lot of boilerplate. There were/are great alternatives to Redux but each seems to have its own complexities and/or different mannerisms that can feel, at times, "too much".
While the hooks feature provides a much easier approach to state management, its focus is really at the component level and not necessarily at the application level. But if you think about it, this is a very good thing and makes perfect sense. After all, React is more a library and less a framework, so it should confine itself to the component landscape along with the composition idioms that breathe life into that landscape.
So what are we to do if there is application state that we really need to share across multiple components, especially deeply nested sub-components?
All of the pre-Hooks era options are certainly still available:
But are any of these really well suited for a complext, real-world, single-page Web application? The authors of this library believe that the answer is no, with the possible exception of the Context API, but it just feels yucky to do this for application state that isn't pervasively global.
The authors of this library are responsible for a large, complex set of single-page applications, which demand (if we want to keep our sanity) that we keep all of our component/sub-component structure completely logical while being able to dispatch state and receive state without resorting to any of the aforementioned approaches.
To solve the problem, we wrote treble-hook, which very simply allows any component in an app to subscribe to state from anywhere in the tree and subsequently publish state at-will so that other components anywhere in the tree picks up those changes. Sounds like this would take a complicated library, right? Nope. Because treble-hook takes advantage of the power of hooks architecture, this functionality is possible via a library with zero hard dependencies and less than 3KB of code.
Even though treble-hook was written in Typescript and thus is the preferred way to consume the library, types in Typescript are compile-time only, meaning they cannot be enforced at runtime without not-so-perfect third party libraries. As such, it's possible for one component to subscribe to a topic using a completely different type another subscribing component. If this were to happen, it would result in unexpected side-effects, for sure.
To mitigate this, it's a strongly suggested best practice to pre-define your state types with one or more enums and interfaces. This, of course, assumes you are writing your app in Typescript...and if you're not, you really should. But you don't have to. Regardless, like in Ghost Busters, you need to exercise extreme discipline and ensure that your proton packs don't cross streams. In other words, make sure that the data type published to a topic remains consistent throughout your application.
See the example code (coming soon) for a way to use an enum/interface to help enforce this policy to publish responsibly.
Coming soon.
Awesome UX Dev team at Igneous Systems.
Coming soon.
Coming soon.
MIT
FAQs
Get hooked on simple subscribe-and-publish in ReactJS.
The npm package treble-hook receives a total of 34 weekly downloads. As such, treble-hook popularity was classified as not popular.
We found that treble-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.