robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.63k stars 135 forks source link

fix example #62

Closed dartess closed 3 years ago

dartess commented 4 years ago

Hello!

The example in README doesn't work, because at the time of the first render there is one element in the array (null). I rewrote the example in a slightly more react style.

robtaussig commented 4 years ago

Hi! Sorry for that late reply.

First, good catch regarding the bug, and thank you for submitting a PR to fix it. That said, I'm interested in your thoughts on replacing the useRef + useMemo combination for messageHistory with useState + useEffect. The latter results in every new message updating your component twice, no?

I definitely understand why the former pattern raised a flag to you, however (I remember feeling apprehensive about creating a ref just to store messages), but since the examples in the README are likely to be used as a starting point by developers using the library, I want to make sure we get it right.

Once we discuss and agree on a pattern, I will be happy to merge:)

dartess commented 4 years ago

Yeah, you're right. I need some time for find better solution.