/*
* 包含所有action的type名称常量
* */
//添加评论
export const ADD_COMMENT = 'add_comment';
//删除评论
export const DELETE_COMMENT = 'delete_comment';
//接收评论数组
export const RECEIVE_COMMENTS = 'receive_comments';

export const INCREMENT = 'increment';

action-types.js

/*
* 包含了所有的action creator(action的工厂函数)
* */
import {ADD_COMMENT, DELETE_COMMENT, RECEIVE_COMMENTS} from './action-types'
// 同步添加
export const addComment = (comment) => (
    {type: ADD_COMMENT, data: comment}
)
// 同步删除
export const deleteComment = (index) => (
    {type: DELETE_COMMENT, data: index}
)
// 同步接收comments
const receiveComments = (comments) => ({
    type: RECEIVE_COMMENTS,
    data: comments
})

// 异步从后台获取数据
export const getComments = () => {
    return dispatch => {
        // 模拟发送ajax请求异步获取数据
        setTimeout(() => {
            const comments = [
                {username: 'Tom', content: 'React挺好的!'},
                {username: 'Jack', content: 'React太难了!'},
                {username: 'Jensen', content: '干就完了!'}
            ]
            //分发一个同步的action
            dispatch(receiveComments(comments))
        }, 1000)
    }
}

actions.js

/*
* 包含n个reducer函数(根据老的state和action返回一个新的state)
* */
import {combineReducers} from 'redux'
import {ADD_COMMENT, DELETE_COMMENT, RECEIVE_COMMENTS, INCREMENT} from './action-types'

function counter(state = 0, action) {
    console.log('counter()', state, action)
    switch (action.type) {
        case INCREMENT:
            return state + action.data
        case DELETE_COMMENT:
            return state - action.data
        default:
            return state
    }
}

const initComments = []

function comments(state = initComments, action) {
    switch (action.type) {
        case ADD_COMMENT:
            return [action.data, ...state]
        case DELETE_COMMENT:
            return state.filter((comment, index) => index !== action.data)
        case RECEIVE_COMMENTS:
            return action.data
        default:
            return state
    }
}

export default combineReducers({
    counter, //指定reducer对应的属性
    comments
})
// redux向外暴露的state是什么结构?
// 是一个对象{counter:2,comments:[]}

reducers.jsx

/*
* redux最核心的管理对象store
* */
import {createStore, applyMiddleware} from 'redux'
import reducers from './reducers'
import thunk from 'redux-thunk'

export default createStore(
    reducers,
    applyMiddleware(thunk)
)

store.js

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'

import store from './redux/store'
import App from './containers/app/app'

ReactDOM.render((
    <Provider store={store}>
        <App/>
    </Provider>
), document.getElementById('root'));

index.js

import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'

import CommentAdd from '../../components/comment-add/comment-add'
import CommentList from '../../components/comment-list/comment-list';
import {addComment, deleteComment, getComments} from '../../redux/actions'

class App extends React.Component {
    //定义数据
    static propTypes = {
        comments: PropTypes.array.isRequired,
        addComment: PropTypes.func.isRequired,
        deleteComment: PropTypes.func.isRequired,
        getComments: PropTypes.func.isRequired
    }

    componentDidMount() {
        //异步获取所有评论数组
        this.props.getComments()
    }

    render() {
        const {comments, addComment, deleteComment} = this.props
        return (
            <div>
                <header className="site-header jumbotron">
                    <div className="container">
                        <div className="row">
                            <div className="col-xs-12">
                                <h1>请发表对React的评论</h1>
                            </div>
                        </div>
                    </div>
                </header>
                <div className="container">
                    <CommentAdd addComment={addComment}/>
                    <CommentList comments={comments} deleteComment={deleteComment}/>
                </div>
            </div>

        )
    }
}

export default connect(
    state => ({comments: state.comments}),// 说明state就是comments数组
    {addComment, deleteComment, getComments}
)(App)

app.jsx

45_redux_comment应用_redux版本_异步功能的更多相关文章

  1. 44_redux_comment应用_redux版本_同步功能

    项目结构: components里面的东西没变,将app.jsx移动至containers中 /* * 包含所有action的type名称常量 * */ //添加评论 export const ADD ...

  2. Spring异步功能

    使用 Spring 的异步功能时,实质是使用的 Servlet3 及以上版本的异步功能. Spring 的异步处理机制需要在 web.xml 中全部的 servlet 和 filter 处配置 < ...

  3. C#各版本新增加功能

    本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...

  4. 【转】C#各版本新增加功能

    本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...

  5. oracle_单向函数_数字化功能

    oracle_单向函数_数字化功能 1.abs(x)   为了获得x绝对值 2.ceil(x)   用于获得大于或等于x的最小整数. 3.floor(x)   用于获得小于或等于x 4.mod(x,y ...

  6. python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学

    首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...

  7. java web开发_购物车功能实现

    java web开发_购物车功能实现 之前没有接触过购物车的东东,也不知道购物车应该怎么做,所以在查询了很多资料,总结一下购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: ...

  8. unix网络编程第2版(卷1)_第6章_同步_异步

    第6章 I/O复用:select和poll函数 6.1概述 在5.12节中,我们看到TCP客户同时处理两个输入:标准输入和TCP套接口.我们遇到的问题是客户阻塞于(标准输入上的)fgets调用,而服务 ...

  9. 爬虫必知必会(4)_异步协程-selenium_模拟登陆

    一.单线程+多任务异步协程(推荐) 协程:对象.可以把协程当做是一个特殊的函数.如果一个函数的定义被async关键字所修饰.该特殊的函数被调用后函数内部的程序语句不会被立即执行,而是会返回一个协程对象 ...

随机推荐

  1. CCNet: Criss-Cross Attention for Semantic Segmentation 里的Criss-Cross Attention计算方法

    论文地址:https://arxiv.org/pdf/1811.11721v1.pdf  code address: https://github.com/speedinghzl/CCNet 相关论文 ...

  2. Java高级特性 第9节 Socket机制

    一.Socket简介 1.Socket概述 Java最初是作为网络编程语言出现的,它对网络的高度支持,使得客户端和服务器流畅的沟通变成现实.而在网络编程中,使用最多的就是Socket,每一个实用的网络 ...

  3. MySQL常用命令(数据库,表相关的命令)

    数据库相关命令 显示数据库列表 mysql> SHOW  DATABASES; 创建数据库 mysql> CREATE  DATABASE  库名; 如下,创建一个名为crashcours ...

  4. angular2的ElementRef在组件中获取不到

    angular2的ElementRef在组件中获取不到 angular2不推荐操作dom,但是实际应用中不可避免的需要使用到dom操作,怎么操作,官方文档提供了一系列api(ElementRef,Vi ...

  5. 探索C++头文件解析方法

    最近一直在搞基于SWIG的C++接口翻译Java代码的工作.SWIG内部基于Bison(Yacc)的C/C++解析器,最近纠结于SWIG不能解析C++构造函数中的默认初始化赋值操作,想找一个能够补充此 ...

  6. DiscuzX /source/function/function_core.php通用核心函数库文件分析

    ... <?php /** * [Discuz!] (C)2001-2099 Comsenz Inc. * This is NOT a freeware, use is subject to l ...

  7. WAMP中mysql服务突然无法启动 解决方法

    wamp的mysql服务前两天还能用,今天突然不能无法启动了,原因可能是近期电脑强行关机,删除wamp\bin\mysql\mysql5.6.17\data目录下的ib_logfile0和ib_log ...

  8. Linux操作汇总

    添加环境变量 CMD,或添加到/etc/profile,或添加到各自账户下的~/.profile.(后两种需要重新source) export PATH=/home/alan/Desktop/arm- ...

  9. MySQL-count(*)和count(1)的查询区别

    一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...

  10. Java核心-多线程-并发控制器-Exchanger交换器

    1.基本概念 Exchanger,从名字上理解就是交换.Exchanger用于在两个线程之间进行数据交换,注意也只能在两个线程之间进行数据交换. 线程会阻塞在Exchanger的exchange方法上 ...