@exiasr/use-socketio
v1.0.2-dev.1
Published
React hooks to use with https://socket.io/
Downloads
2
Readme
Use socket.io using React Hooks (React v16.8+)
Usage
Installation
$ yarn add use-socketio
In your code
import { ClientSocket, useSocket } from "use-socketio";
const Twitter = () => {
const [tweets, setTweet] = useState([]);
const socket = useSocket("tweet", newTweet =>
setTweet([newTweet, ...tweets])
);
return tweets.length ? (
<ul>
{tweets.map(tweet => (
<li key={tweet.id}>{tweet.text}</li>
))}
</ul>
) : (
<p>Actually waiting for the websocket server...</p>
);
};
const App = () => (
<ClientSocket url="https://some-tweet-socket-listener/">
<Twitter />
</ClientSocket>
);