前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React第三方组件4(状态管理之Reflux的使用①简单使用)

React第三方组件4(状态管理之Reflux的使用①简单使用)

作者头像
前端人人
发布2018-04-11 16:56:35
1.2K0
发布2018-04-11 16:56:35
举报
文章被收录于专栏:前端人人前端人人

本教程总共5篇,每日更新一篇,请关注我们!你可以进入历史消息查看以往文章,也敬请期待我们的新文章!

1、React第三方组件4(状态管理之Reflux的使用①简单使用)---2018.03.13

2、React第三方组件4(状态管理之Reflux的使用②TodoList上)---2018.03.14

3、React第三方组件4(状态管理之Reflux的使用③TodoList中)---2018.03.15

4、React第三方组件4(状态管理之Reflux的使用④TodoList下)---2018.03.16

5、React第三方组件4(状态管理之Reflux的使用⑤异步操作)---2018.03.19

开发环境:Windows 8,node v8.9.1,npm 5.5.1,WebStorm 2017.2.2

先说下ReFlux

引用:https://segmentfault.com/a/1190000004843954 一个简单的单向数据流应用库,灵感来自于ReactJS Flux. ╔═════════╗ ╔════════╗ ╔═════════════════╗ ║ Actions ║──>║ Stores ║──>║ View Components ║ ╚═════════╝ ╚════════╝ ╚═════════════════╝ ^ │ └──────────────────────────────────────┘ 同React Flux比较 refluxjs的目标是为了让我们更容易的搭建Web应用程序。 相同点 1、有actions 2、有stores 3、单向数据流 不同点 1、通过内部拓展actions的行为,移除了单例的dispatcher 2、stores可以监听actions的行为,无需进行冗杂的switch判断 3、stores可以相互监听,可以进行进一步的数据聚合操作,类似于,map/reduce 4、waitFor被连续和平行的数据流所替代

我们直接撸码!

先安装reflux

npm i -S reflux

1、我们建立下reflux目录,及reflux1目录,和Index.jsx

2、reflux下的Index.jsx代码

import React from 'react';
import {HashRouter, Route, NavLink, Redirect} from 'react-router-dom';
import ReFlux1 from './reFlux1/Index'

const Index = ({match}) =>
    <HashRouter>
        <div>
            <div className="nav">
                <NavLink to="/ReFlux/ReFlux1" activeClassName="selected">ReFlux1</NavLink>
            </div>
            <Route exact path={`${match.url}`}
                   render={() => (<Redirect to={`${match.url}/ReFlux1`}/>)}/>
            <Route path={`${match.url}/ReFlux1`} component={ReFlux1}/>
        </div>
    </HashRouter>
;

export default Index;

3、reflux1下建立Index.jsx

import React from 'react'
import Reflux from 'reflux'
import Action from './Action'
import Store from './Store'

class Index extends Reflux.Component {
    constructor(props) {
        super(props);
        this.store = Store;
    }

    render() {
        return (
            <div className="todoList">
                {this.state.num}
                <button onClick={() => Action.add()}>+</button>
            </div>
        );
    }
}

export default Index;

注意这几个框起来的!

4、建立Store.js

import Reflux from 'reflux'
import Action from './Action'

class Store extends Reflux.Store {
    constructor() {
        super();
        this.listenables = Action;
        this.state = {
            num: 0
        }
    }

    onAdd() {
        this.setState({num: this.state.num + 1});
    }
}

export default Store;

5、建立Action.js

import Reflux from 'reflux'

let Action = Reflux.createActions([
    'add'
]);
export default Action;

到这里就结束了,相对比较简单!

6、查看浏览器

如果你有什么问题,可以在下方留言给我们!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端人人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档