Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

treble-hook

Package Overview
Dependencies
Maintainers
6
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

treble-hook - npm Package Compare versions

Comparing version 2.0.0-alpha.8 to 2.0.0

2

package.json
{
"name": "treble-hook",
"version": "2.0.0-alpha.8",
"version": "2.0.0",
"description": "Get hooked on simple subscribe-and-publish in ReactJS.",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

<div>
<img height="180" alt='treble-hook' src='https://raw.githubusercontent.com/rollercodester/treble-hook/master/doc-assets/treble-hook-3.png'/>
<p>
<i>Super easy way to get "hooked" on subscribe-and-publish in React with no dependencies and no cruft.</i>
</p>
<h3>
<i>Simple lightweight state management in React.</i>
</h3>
<br />
<h3 style={{ color: 'red' }}>IMPORTANT: Upgrading from v1 to v2 includes breaking changes; see docs for more info.</h3>
<br />
<div style="float:left;">

@@ -18,7 +20,6 @@ <a href="https://www.npmjs.com/package/treble-hook" rel="nofollow"><img src="https://img.shields.io/npm/v/treble-hook.svg?style=flat" alt="version"></a>

<p></p>
## Quick Start
## Installation
### Installation
`yarn add treble-hook`

@@ -30,4 +31,158 @@

### Usage
## Usage
```jsx
import trebleHook, { usePubSub } from 'treble-hook'
const Welcome = () => {
const [guestName] = usePubSub('guest')
return (
<h3>Welcome to treble-hook, {guestName}!</h3>
)
}
const GuestEntry = () => {
const [_, pubGuestName] = usePubSub('guest')
return (
<div>
<input
type="text"
onChange={(e) => { pubGuestName(e.target.value) }}
/>
</div>
)
}
const App = () => {
trebleHook.addTopic('guest', 'amigo')
const GuestPublisher = trebleHook.getPublisher()
return (
<GuestPublisher>
<GuestEntry />
<br />
<Welcome />
</GuestPublisher>
)
}
```
## API
See docs for API.
## Codesandbox Examples
Guest Sign-in
Classic ToDo App
Crack that Code Game
## Defacto ToDo App Example
### App.jsx
```tsx
import React from 'react'
import trebleHook from 'treble-hook'
import AddToDo from './AddToDo'
import ToDoList from './ToDoList'
import Footer from './Footer'
trebleHook.addTopic('todos', [])
trebleHook.addTopic('filter', 'all')
const ToDoPublisher = trebleHook.getPublisher()
export default function App() {
return (
<ToDoPublisher>
<AddToDo />
<ToDoList />
<Footer />
</ToDoPublisher>
)
}
```
### ToDoList.jsx
```tsx
import React from 'react'
import { usePubSub } from 'treble-hook'
import ToDo from './todo'
export default function ToDoList() {
const [todos, pubToDos] = usePubSub('todos')
const [filter] = usePubSub('filter')
const filteredToDos = todos.filter((todo) => {
switch (filter) {
case 'completed': return todo.completed
case 'active': return !todo.completed
default: return true
}
})
return (
<ul>
{filteredToDos.map(todo =>
<ToDo
key={todo.id}
{...todo}
onClick={() => {
todo.completed = !todo.completed
pubToDos([...todos])
}}
/>
)}
</ul>
)
}
```
### ToDo.jsx
```tsx
import React from 'react'
import { usePubSub } from 'treble-hook'
export default function ToDo({ todo }) {
const [todos, pubToDos] = usePubSub('todos')
const [filter] = usePubSub('filter')
const filteredToDos = todos.filter((todo) => {
switch (filter) {
case 'completed': return todo.completed
case 'active': return !todo.completed
default: return true
}
})
return (
<ul>
{filteredToDos.map(todo =>
<ToDo
key={todo.id}
{...todo}
onClick={() => {
todo.completed = !todo.completed
pubToDos([...todos])
}}
/>
)}
</ul>
)
}
```
Contrived sign-in component along with component that displays active user (signed-in user).

@@ -34,0 +189,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc