Created
April 15, 2015 21:25
-
-
Save mattpodwysocki/0697e8c7d317998170da to your computer and use it in GitHub Desktop.
Getting a list of users with RxJS and JSX
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv='Content-type' content='text/html; charset=utf-8'> | |
<title>Basic Example with JSX</title> | |
<link rel="stylesheet" href="base.css" /> | |
</head> | |
<body> | |
<h1>ReactJs + RxJs</h1> | |
<div id="container"> | |
<p> | |
If you can see this, React is not working right. | |
If you checked out the source from GitHub make sure to run <code>grunt</code>. | |
</p> | |
</div> | |
<h4>Example Details</h4> | |
<p>This is written with JSX and transformed in the browser.<p> | |
<script src="https://code.jquery.com/jquery-2.1.3.js"></script> | |
<script src="http://fb.me/react-0.13.1.min.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.13.1.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.1/rx.all.js"></script> | |
<script type="text/jsx"> | |
function getData(url) { | |
return jQuery.ajax({ | |
url: url, | |
dataType: 'jsonp' | |
}).promise(); | |
} | |
var subject = new Rx.BehaviorSubject('https://api.github.com/users'); | |
var responseStream = subject.flatMap(getData); | |
var ExampleApplication = React.createClass({ | |
getInitialState: function() { | |
return { data: [] }; | |
}, | |
componentDidMount: function() { | |
var self = this; | |
responseStream.subscribe(function (response) { | |
self.setState( { data: response.data } ); | |
self.forceUpdate(); | |
}); | |
}, | |
getMoarUsers: function(e){ | |
var randomOffset = 'https://api.github.com/users?since=' + Math.floor(Math.random()*500); | |
subject.onNext(randomOffset); | |
}, | |
render: function() { | |
var message = this.state.data.map(function (user) { | |
return ( | |
<div> | |
{user.login} | |
</div> | |
); | |
}); | |
return (<div> | |
{message} | |
<div><button onClick={ this.getMoarUsers }>Get moar users</button></div> | |
</div>); | |
} | |
}); | |
React.render( | |
<ExampleApplication />, | |
document.getElementById('container') | |
); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A bit off topic, but likely this would be quite helpful for anyone who are starting to work with React and Rx.
It is what I learned while using Rx with React:
https://gist.github.com/zxbodya/20c63681d45a049df3fc
In short - It is better to create stream of elements to render, instead of creating complicated component that will render data directly from streams.
Just to illustrate what I mean - example above can be rewritten as following:
Just some of benefits:
Rx.Observable.return([]).merge(responseStream)
above by justresponseStream
, and first render will happen after data will be received)