Created
February 14, 2016 20:42
-
-
Save spencercarli/0914bee40c592b651595 to your computer and use it in GitHub Desktop.
Meteor Authentication from React Native - The UI
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
// RNApp/app/loggedIn.js | |
import React, { | |
View, | |
Text | |
} from 'react-native'; | |
import Button from './button'; | |
import ddpClient from './ddp'; | |
export default React.createClass({ | |
getInitialState() { | |
return { | |
posts: {} | |
} | |
}, | |
componentDidMount() { | |
this.makeSubscription(); | |
this.observePosts(); | |
}, | |
observePosts() { | |
let observer = ddpClient.observe("posts"); | |
observer.added = (id) => { | |
this.setState({posts: ddpClient.collections.posts}) | |
} | |
observer.changed = (id, oldFields, clearedFields, newFields) => { | |
this.setState({posts: ddpClient.collections.posts}) | |
} | |
observer.removed = (id, oldValue) => { | |
this.setState({posts: ddpClient.collections.posts}) | |
} | |
}, | |
makeSubscription() { | |
ddpClient.subscribe("posts", [], () => { | |
this.setState({posts: ddpClient.collections.posts}); | |
}); | |
}, | |
handleIncrement() { | |
ddpClient.call('addPost'); | |
}, | |
handleDecrement() { | |
ddpClient.call('deletePost'); | |
}, | |
render() { | |
let count = Object.keys(this.state.posts).length; | |
return ( | |
<View> | |
<Text>Posts: {count}</Text> | |
<Button text="Increment" onPress={this.handleIncrement}/> | |
<Button text="Decrement" onPress={this.handleDecrement}/> | |
</View> | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment