-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,70 @@ | ||
# react-native-actioncable | ||
ActionCable for react native | ||
ActionCable for react native. | ||
|
||
Just import `actioncable` module, and let it compatiblely with `react native`. | ||
|
||
# Install | ||
|
||
``` | ||
npm install --save react-native-actioncable | ||
``` | ||
|
||
# Usage | ||
|
||
Two ways to use `react-native-actioncable` | ||
|
||
## Use ActionCable methods | ||
|
||
```jsx | ||
import ActionCable from 'react-native-actioncable' | ||
|
||
const cable = ActionCable.createConsumer('ws://localhost:3000/cable') | ||
|
||
// ... Other code | ||
``` | ||
|
||
## Use ActionCableProvider | ||
|
||
In outer container: | ||
|
||
```jsx | ||
import { ActionCableProvider } from 'react-native-actioncable' | ||
|
||
export default function Container (props) { | ||
return ( | ||
<ActionCableProvider url='ws://localhost:3000/cable'> | ||
<MyApp /> | ||
</ActionCableProvider> | ||
) | ||
} | ||
``` | ||
|
||
In some UI screen: | ||
|
||
```jsx | ||
import React, { Component, PropTypes } from 'react' | ||
import ActionCable from 'react-native-actioncable' | ||
|
||
export default class ChatRoom extends Component { | ||
static contextTypes = { | ||
cable: PropTypes.object.isRequired | ||
}; | ||
|
||
componentDidMount () { | ||
this.subscription = this.context.cable.subscriptions.create( | ||
'ChatChannel', | ||
{ | ||
received (date) { | ||
console.log(data) | ||
} | ||
} | ||
) | ||
} | ||
|
||
componentWillUnmount () { | ||
this.subscription && | ||
this.context.cable.subscriptions.remove(this.subscription) | ||
} | ||
|
||
// ... Other code | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters