Skip to content

Instantly share code, notes, and snippets.

@lifuzu
Created June 26, 2015 17:09
Show Gist options
  • Save lifuzu/8e7ee7c647dd52188351 to your computer and use it in GitHub Desktop.
Save lifuzu/8e7ee7c647dd52188351 to your computer and use it in GitHub Desktop.
react native with immutable and set state understanding
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