我嘗試使用websockets實現類似於聊天應用程序的內容。但是當websocket得到消息時,沒有組件正在重新渲染。現在請查看我的代碼Reactjs使用套接字:當套接字接收到消息時重新渲染
# The main ancestor, chat application
@Chat = React.createClass
componentWillMount: ->
@setState signals: [] # the list of signals received by websockets
socket = new WebSocket('ws://localhost:19108')
socket.onmessage = (event) =>
@state.signals.push { message: event.data, direction: 'in' }
console.log @state.signals # it regularly write a message to console after receiving socket message
@setState socket: socket
render: ->
<div>
<div className="row">
<div className="col-md-12">
<SignalList signals={@state.signals} /> # I pass a signals here
</div>
</div>
</div>
$ ->
React.render(<Chat />, document.getElementById('container'))
信號列表嘗試只是爲了顯示的props
變化,但沒有成功。這仍然是一個空數組
@SignalList = React.createClass
render: ->
<div>{ @props }</div>
我想的WebSockets收到的每個消息後props
重新描繪。我怎樣才能做到這一點?
謝謝!但我認爲,另一個例子更短 – asiniy
很高興我可以幫助至少:) –