Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dnamsons/rails-hooks

Rails javascript APIs turned into Hooks for use in functional React components
https://github.com/dnamsons/rails-hooks

actioncable rails

Last synced: about 1 month ago
JSON representation

Rails javascript APIs turned into Hooks for use in functional React components

Awesome Lists containing this project

README

        

# Rails hooks

[![Version][version-badge]][package]

## Installation

with npm

```sh
npm install rails-hooks
```

with yarn

```sh
yarn add rails-hooks
```

## API

- [useCable](#usecable)

### `useCable`

Requires for `CableProvider` to be set somewhere above the component where `useCable` will be used.

```tsx
import React from 'react'
import { CableProvider } from 'rails-hooks'
import { createConsumer } = '@rails/actioncable'

const App = () => {
const consumer = createConsumer('optional-url.com')

return(

...

)
}
```

Then `useCable` can be used like this:

```tsx
import { useCable } from 'rails-hooks'

const [messages, setMessages] = useState([])

const chatChannel = useCable(
{ channel: 'ChatChannel', room: 'Best room' },
{
received(data) {
setMessages(prevMessages => [...prevMessages, data])
},
}
)

chatChannel.send({ sent_by: 'Paul', body: 'This is a cool chat app.' })
```

[version-badge]: https://img.shields.io/npm/v/rails-hooks.svg?style=flat-square
[package]: https://www.npmjs.com/package/rails-hooks