Last active
March 5, 2016 17:54
-
-
Save vy-nguyen/8d4d58cca09eb1a0c602 to your computer and use it in GitHub Desktop.
React sample code
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 Login = React.createClass({ | |
mixins: [Reflux.listenTo(userStore, 'resetForm')], | |
resetForm: function() { | |
this.setState({ | |
submitted: false, | |
}); | |
this.refs.email.getDOMNode().value = ''; | |
this.refs.password.getDOMNode().value = ''; | |
this.refs.submit.getDOMNode().disabled = false; | |
}, | |
var SinglePost = React.createClass({ | |
addComment: function(e) { | |
e.preventDefault(); | |
if (!this.props.user.isLoggedIn) { | |
actions.showLoginOverlay(); | |
return; | |
} | |
// submit comment... | |
}, | |
// render, etc... | |
}); | |
// etc... | |
}); | |
var request = window.superagent; | |
testUrl = 'http://jsonplaceholder.typicode.com/users'; | |
var Actions = Reflux.createActions({ | |
'load': {children: ['completed', 'failed']} | |
}); | |
Actions.load.listen(function() { | |
var thisAction = this; | |
request.get(testUrl) | |
.end(function(err, res) { | |
if (err) { return thisAction.failed(err) } | |
thisAction.completed(res); | |
}); | |
}); | |
var myStore = Reflux.createStore({ | |
data: {users: [{id:1,name:'nobody'}]}, | |
init(){ | |
this.listenTo(Actions.load.completed,this.onLoadCompleted); | |
}, | |
onLoadCompleted(res){ | |
this.data.users = JSON.parse(res.text); | |
this.trigger(this.data); | |
}, | |
getInitialState() { | |
return this.data; | |
} | |
}); | |
var App = React.createClass({ | |
mixins: [Reflux.connect(myStore)], | |
render() { | |
return (<div> | |
{this.state.users.map(person => { | |
return (<div key={person.id}> | |
<div>{person.name}</div> | |
</div>) | |
})} | |
</div>); | |
} | |
}) | |
React.render(<App/>, document.getElementById('example')); | |
Actions.load(); | |
http://henleyedition.com/building-an-app-using-react-and-refluxjs/ | |
var ReactNews = React.createClass({ | |
mixins: [ | |
Reflux.listenTo(actions.showLoginOverlay, 'showLoginOverlay') | |
], | |
showLoginOverlay: function() { | |
this.setState({ | |
showOverlay: true | |
}); | |
} | |
// etc... | |
}); | |
https://github.com/echenley/react-news |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
actions.addComment.preEmit = function(comment) {
commentsRef.push(comment, function(error) {
if (error === null) {
actions.updateCommentCount(comment.postId, 1);
}
});
};