由于ReactJS只是视图层并由他自己工作,在构建SPA (单页应用程序)时,需要为完整的堆栈ReactJS套件使用哪些附加库--数据层、与服务器的通信(AJAX调用、REST)等等?
它们是否有任何可用的ReactJS全堆栈框架(类似于AngularJS)?
发布于 2015-06-22 21:46:12
仅ReactJS就为您提供了DOM呈现,但Facebook还创建了Flux
,为您提供了一个可以在其中工作的架构。通过遵循Flux制定的规则,您现在拥有了一个带有DOM呈现、数据模型和两者之间通信的SPA。
当然,您将使用Flux构建的SPA是独立的。通量不足以为您提供执行AJAX请求的工具。你需要另一个图书馆。然而,NodeJS社区充满了AJAX实现,我可能会更喜欢它。
superagent
是一个相当受欢迎的。(这就是我所用的。)您可能会注意到它不支持承诺,所以您也可以查看superagent-bluebird-promise
,它将superagent
封装在bluebird
承诺库中。
另一个注意事项,如果您要使用Flux,我还建议您使用越来越多的包装库中的一个,它将帮助您减少样板。看看Reflux
。
一个完整的周期可能是这样的..。
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;
发布于 2015-07-24 15:28:42
你可以看着
http://martyjs.org/是Flux应用体系结构的一个实现。
(es6支持/响应本机支持/高阶组件(容器:abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750))
发布于 2015-10-29 19:44:58
您可能想在GitHub上搜索“工具包”。使用React构建温泉疗养场的流行技术包括:
至于初学者工具包,下面列出了一个有趣的React样板http://habd.as/awesome-react-boilerplates列表
https://stackoverflow.com/questions/30978372
复制相似问题