首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >全栈ReactJS套件

全栈ReactJS套件
EN

Stack Overflow用户
提问于 2015-06-22 11:06:25
回答 4查看 968关注 0票数 3

由于ReactJS只是视图层并由他自己工作,在构建SPA (单页应用程序)时,需要为完整的堆栈ReactJS套件使用哪些附加库--数据层、与服务器的通信(AJAX调用、REST)等等?

它们是否有任何可用的ReactJS全堆栈框架(类似于AngularJS)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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;
票数 6
EN

Stack Overflow用户

发布于 2015-07-24 15:28:42

你可以看着

http://martyjs.org/是Flux应用体系结构的一个实现。

(es6支持/响应本机支持/高阶组件(容器:abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750))

票数 4
EN

Stack Overflow用户

发布于 2015-10-29 19:44:58

您可能想在GitHub上搜索“工具包”。使用React构建温泉疗养场的流行技术包括:

至于初学者工具包,下面列出了一个有趣的React样板http://habd.as/awesome-react-boilerplates列表

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30978372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档