Complete ReactJS package
How ReactJS is only a presentation layer and works on its own, what additional libraries should be used for a complete ReactJS package - data layer, server communication (AJAX, REST calls), etc. when creating a SPA (single page application)
Are there any complete ReactJS frameworks available (like AngularJS)?
source to share
Only ReactJS gives you DOM rendering, but Facebook also created Flux
which gives you the architecture you are working in. Following the rules set by Flux, you now have a SPA with DOM rendering, data models, and communication between them.
Of course, the SPA you'll be building with Flux is self-contained. Flux doesn't go as far as providing you with the tools to make AJAX requests. You will need another library for this. However, the NodeJS community is so full of AJAX implementations that I might have preferred it.
superagent
is quite popular. (This is what I am using.) You may have noticed that it does not support promises, so you can also check superagent-bluebird-promise
which wraps superagent
with the promises library bluebird
.
Another note, if you're going to be using Flux, I also recommend pulling out one of the growing number of wrapper libraries to help you shrink the boilerplate. Check it out Reflux
.
The complete cycle might look like this:
RecordList.jsx
const React = require('react');
const Reflux = require('reflux');
const RecordStore = require('../stores/RecordStore');
const RecordActions = require('../actions/RecordActions');
const RecordList = React.createClass({
mixins: [
// auto-magically create event listeners to state change to re-render
Reflux.connect(RecordStore)
],
// There is no `getInitialState()` here, but the one in `RecordStore` is inherited.
// load the initial data
componentDidMount: function () {
RecordActions.load();
},
// render records as a list
render: function () {
return (
<li>
{
this.state.records.map(function (record) {
return <ul>{record.name}</ul>;
})
}
</li>
);
}
});
module.exports = RecordList;
RecordActions.js
const Reflux = require('reflux');
const request = require('superagent-bluebird-promise');
const RecordActions = Reflux.createActions({
// create an action called 'load' and create child actions of 'completed' and 'failed'
load: {asyncResult: true}
});
// set up promise for loading records
RecordActions.load.listenAndPromise(() =>
request.get('/records')
.type('application/json')
.then(res => res.body)
);
module.exports = RecordActions;
RecordStore.js
const Reflux = require('reflux');
const RecordActions = require('../actions/RecordActions');
/**
* storage for record data
*/
const RecordStore = Reflux.createStore({
// listen for events from RecordActions (Reflux)
listenables: RecordActions,
init: function () {
this.data = {
records: []
};
},
// facilitate initializing component state with store data
getInitialState: function () {
return this.data;
},
/*
* all records
*/
getRecords: function () {
return this.data.records;
},
// handle successful load of records
onLoadCompleted: function (response) {
this.data.records = response;
this.trigger(this.data);
},
// handle failure to load records
onLoadFailed: function (err) {
console.error('Failed to load records', err.toString());
}
});
module.exports = RecordStore;
source to share
U could see
http://martyjs.org/ which is an implementation of the Flux Application Architecture.
(es6 support / native support / higher order components (containers: https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750 ))
source to share
You might want to do a search on GitHub for "starter kit answers". The popular technology stack for building SPA with React consists of:
- Stream implementation -> you can pick one here https://github.com/voronianski/flux-comparison
- React Router https://github.com/rackt/react-router
- Webpack + Webpack Dev Server, Babel, Gulp, ESLint + CSSLint, LESS / Sass, etc.
- Jest / Mocha + Chai + Sinon
- Netflix Falcor https://github.com/Netflix/falcor (developer preview)
- some React elements http://material-ui.com / https://react-bootstrap.github.io / http://react-components.com
Regarding starter kits, here is an interesting list of reactive templates http://habd.as/awesome-react-boilerplates
source to share