React Rebix React 的单向数据流框架

程序名称:React Rebix

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

React Rebix 介绍

React的一个单向数据流框架。

优点

内部实现依赖于Redux。但是简化了Redux的使用方法。

  1. action层只需要返回action方法的处理结果,无需action去dispatch处理的结果。

  2. store层,无需写大量的switch判断,而是采用reflux的风格,直接使用onXXXX来响应Action的处理。

  3. view层无需自己去依赖action和store层,而是直接采用简单的配置,就能自动将action和store中的数据绑定到组件的props中。

  4. view层中调用的action方法如果是异步方法会将返回值中的promise对象透传到view层。

  5. action层和view层,都可以直接访问store中的Get方法。但是view层和action层,都无法访问store中的非get方法。这样既能保证调用的灵活性,又能保证数据流的单向流动。

  6. 跟其他框架相比,用户省去了大量自己手动书写的对数据的pub/sub的代码。

安装

npm install --save react-rebix

使用

import Rebix from 'react-rebix';

OR

<script src="/node_modules/react/dist/react.min.js"></script>
<script src="/node_modules/react-dom/dist/react-dom.js"></script>
<script src="/node_modules/react-rebix/dist/react-rebix.min.js"></script>

示例

TodoMVC

https://github.com/ubibi/rebix-todo-demo

https://github.com/luanhaipeng/rebix/tree/master/example/example

Action

Action中可访问Store中的getXXX方法,其他方法不能访问。

支持三种Action

  1. 异步 Action, 一定要返回一个Promise对象

  2. 普通 Action,直接返回处理结果的js对象。

  3. 空Action, 不需要具体的实现,具体操作在Store中完成.

    import Rebix from ‘react-rebix’;
    import UserStore from ’../stores/UserStore’;

    export default Rebix.createActions({

    /*
         * 异步 Action
         * 一定要返回一个Promise对象
    /
        getUserInfo: function (params) {

    //Action 中可以访问Store中的数据。但是只能调用get方法。
            //Store 中的其他方法,不会对外暴露,这样方便了数据的访问,同时又保证了数据的单向流动。
            var userInfo = UserStore.getUserInfo(123);

    return new Promise(function (resolve) {
                setTimeout(function () {
                    //store层使用action.promise字段接受返回值
                    resolve({
                        time: new Date().getTime(),
                        userInfo: userInfo,
                        params: params
                    });
                }, 1000)
            })
        },

    /*
         * 普通 Action
    /
        getUserList: function (params) {
            //store层使用action.payload字段接受返回值
            return [1, 2, 3, params];
        },

    /*
         * 空Action, 不需要具体的实现
         * 具体操作在Store中完成.
    /
        beginEditUserInfo: null,

    /*
         * 空Action, 不需要具体的实现
         * 具体操作在Store中完成.
    /
        endEditUserInfo: null

    });

Store

Store中的数据存储,强烈建议使用immutable,这里为了演示方便,通过Object.assign({}, state)创建了一个新对象。

说明:

  1. 为了保证数据的单向流动,通过CreateStore创建的onXXXX函数,view层和action层根本调用不到。

  2. 为了方便action和view层使用数据,通过CreateStore创建的getXXXX函数,view层和action层都可以调用到。

  3. 一般来说action文件和store文件是一一对应的,但是有时候一个action的处理结果需要几个store层各自处理。这里提供了加井号前缀的方式实现。比如:post#onGetPostList(在UserStore中响应PostAction的结果。)

    import Rebix from ‘react-rebix’;

    export default Rebix.createStore({

    initialState: {
            userList: [],
            postList: []
        },

    //类似Reflux。Action中的处理结束后,会把数据传递给Store
        //这里处理:action中方法 getUserList 的返回值。
        ‘onGetUserList’: function (state, action) {
            console.log(action.status);
            state = Object.assign({}, state);
            var userList = action.payload;
            state.userList = userList;
            return state;
        },

    //处理action中beginEditUserInfo的行为。
        ‘onBeginEditUserInfo’: function (state) {
            state = Object.assign({}, state);
            state.isEditing = true;
            return state;
        },

    //处理action中onEndEditUserInfo的行为。
        ‘onEndEditUserInfo’: function (state) {
            state = Object.assign({}, state);
            state.isEditing = false;
            return state;
        },

    /*
         * 为了响应其它Action方法中的处理,要加#前缀
    /
        ‘post#onGetPostList’: function (state, action) {
            console.log(action.status);
            if (action.status === ‘success’) {
                state = Object.assign({}, state);
                var postList = action.payload;
                state.postList = postList;
            }

    return state;
        },

    /*
         * Get函数,修改state不管用.state内部一般都是使用immutable对象,只有on函数的返回值才能对state进行修改.
         * View 层,可以直接调用Get函数获取Store中的数据,但是无法修改.
         * 在Get函数内部对state的任何修改,都不会生效.
    /
        ‘getUserInfo’: function (state, a, b, c, d) {
            return {
                name: a
            };
        }

    });

Config

通过Config,将action、store等资源集中起来。这样的目的是为了在view层,无需再引入大量的action、store的js文件。

说明:

  1. createConfigure中只有三个配置项。

  2. initialState 是用来做服务端初次数据渲染用的。

  3. actions 所有action的集合。

  4. stores所有stores的结合。

  5. actions和stores中配置的key值基本保证是一一对应的。如下:user和post

    import Rebix from ‘react-rebix’;
    import UserActions from ’../actions/UserActions’;
    import PostActions from ’../actions/PostActions’;
    import UserStore from ’../stores/UserStore’;
    import PostStore from ’../stores/PostStore’;

    export default Rebix.createConfigure({

    initialState: null,

    actions: {
            user: UserActions,
            post: PostActions
        },

    stores: {
            user: UserStore,
            post: PostStore
        }

    });

View

注意:

  1. Action中可访问Store中的getXXX方法,其他方法不能访问。

  2. View层通过Rebix.createComponent将action和store自动绑定到组建的props中。

  3. Store发生了变化,会自动update,因此强烈建议重写shouldComponentUpdate来避免重复渲染。这里跟redux是一样的。

    import React, {PropTypes} from ‘react’;
    import createRebixComponent from ’../config/createRebixComponent’;
    import UserStore from ’../stores/UserStore’;
    import RebixConfigure from ’./RebixConfigure’;

    class Hello extends React.Component {

    constructor(props) {
            super(props);
            this.state = {
                userInfo: {}
            };
        }

    componentDidMount() {
            var that = this;
            var {actions} = that.props;
            var mm = actions.getUserList(1234);
            var mm2 = actions.beginEditUserInfo(1234);

    actions.getPostList(‘absdf’, ‘sdf’).then(function () {
                debugger;
            });

    setTimeout(function () {
                //直接访问Store中的数据
                var userInfo = UserStore.getUserInfo(121);
                that.setState({userInfo: userInfo});
            }, 2000)
        }

    render() {
            var userList = this.props.userList || [];
            var postList = this.props.postList || [];
            var userInfo = this.state.userInfo || {};

    return (
                


                    aaa—{userInfo.name}
                    

                    bbb—
                    {userList.map(function (x) {
                        return 
    {x}

                    })}
                    

                    ccc—
                    {postList.map(function (x) {
                        return 
    {x}

                    })}
                

            );

    }

    }

    export default Rebix.createComponent(RebixConfigure, Hello, {

    actions: {
            getUserList: ‘user.getUserList’,  //请参考config文件中的配置。
            getPostList: ‘post.getPostList’,
            beginEditUserInfo: ‘user.beginEditUserInfo’
        },

    props: {
            userList: ‘user.userList’,
            postList: ‘user.postList’
        }

    });

原理

内部实现还是使用Redux,只有一个唯一的Store,通过connect自动完成对store数据变化的pub/sub机制。

License

MIT

联系我

  1. Email: master@ubibi.cn

React Rebix 官网

https://github.com/luanhaipeng/rebix

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


BBGestureBack Full screen return gesture(全屏手势返回 滑动返回 pop 动画效果) iOS 侧滑返回
Framework7 或者叫 F7 是全功能的绑定 iOS 7 应用的 HTML 框架。Framework7 是免费开源的 HTML
iOS 调试库,支持 iOS8+,无需添加任何代码,方便 iOS 开发和测试。 屏幕截图 使用教程
DarkModeKit是在Apple官方的深色模式发布之前设计和开发的。 它提供了一种机制来支持iOS 11+(包括iOS 13)上的应用程序的暗模式。
SimpleNote iOS 版客户端。SimpleNote 是一款在多平台上发布的笔记应用。 开发准备条件:
Kivy iOS,此工具旨在为 iOS 编译必要的库以运行应用程序并管理 Xcode 项目的创建。
TYDownloadManager是一个iOS的文件下载管理器包,可以提示下载过程中的最新进展和状态的变化。
mruby是一款轻量级的Ruby实现,遵循ISO标准。它可以以解释的形式或者在VM上编译和执行形式运行。
因为涉嫌危害消费者的隐私问题,苹果公司决定停止使用原有的UDID系统,移动应用软件开发者们正在竞相寻找新的替代方案。移动应用销售平台Appsfire目前推出了OpenUDID,它是一个开源版本的UDID。Appsfire声称他们已
EasyIOS 开源至今已经1周年,全新Swift版本今日发布,支持利用HTML来开发IOS应用,支持模拟器实时预览,基于MVVM思想,HTML数据绑定,反射所有
在 iOS 领域使用 Pod 来进行组件化开发时常常需要手动进行一系列的操作,来实现Pod 组件的更新目的。特别是一些业务关联性比较强的 Pod
PokerCard,一款 iOS 多样式弹窗开源库。 Basic Usage import PokerCard class ViewController: UIViewController {
MiaowShow MiaowShow是高仿《喵播APP》的iOS视频直播项目。 项目具体讲解地址 iOS视频直播初窥:高仿<喵播APP>
ZJAttributedText 是高性能轻量级富文本框架 前言 如果遇到上面一个需求, 你会怎么处理, 若干个 UILabel + UIImageView? NSAttributedString拼接? CoreText?
DynamicCocoa,是滴滴 App 架构组自研的 iOS 动态化方案,可以让现有的 Objective-C 代码转换生成中间代码(JS),下发后动态执行。滴滴客户端 App 架构团队表示正在积极准备相关事项,计划于 2017 年初开源。
ObjectiveSupport aims to bring some of the popular Rubyisms found in ActiveSupport to Objective-C. This project originated as a component of
StyleKit 是能让你使用一个简单的 JSON 文件美化你的应用的微框架。 它如何工作?
XCActionBar 是一个用于 Xcoded 的通用生产工具。
Social Go是一个基于iOS平台,在人群中获取小精灵的雷达和匿名聊天App。它使用Swift
Swift版本最新发布 : https://github.com/EasyIOS/EasyIOS- Swift 全新基于MVVM(Model-View-ViewModel)编程模式架构,开启EasyIOS开发函数式编程新篇章。