Created
June 26, 2015 17:09
-
-
Save lifuzu/8e7ee7c647dd52188351 to your computer and use it in GitHub Desktop.
react native with immutable and set state understanding
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
var { Map, List } = require('immutable'); | |
getInitialState: function() { | |
return { | |
data: Map({ input: '', messages: List() }) | |
} | |
}, | |
componentDidMount: function() { | |
this.socket = io('http://localhost:5000', { jsonp:false }); | |
this.socket.on('chat message', (msg) => { | |
// this.state.messages.push(msg); | |
this.setState(prev => ({ | |
data: prev.data.update('messages', list => list.push(msg)) | |
})) | |
this.forceUpdate(); | |
}); | |
}, | |
render: function() { | |
return ( | |
<ScrollView | |
style={styles.scrollView} | |
scrollEventThrottle={200} | |
bounces={false} | |
contentInset={{top: 0}} | |
> | |
{this.state.data.get('messages').map(m => { | |
return <Text style={styles.message}>Josh: {m}</Text> | |
})} | |
</ScrollView> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment