45_redux_comment应用_redux版本_异步功能
/* * 包含所有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版本_异步功能的更多相关文章
- 44_redux_comment应用_redux版本_同步功能
项目结构: components里面的东西没变,将app.jsx移动至containers中 /* * 包含所有action的type名称常量 * */ //添加评论 export const ADD ...
- Spring异步功能
使用 Spring 的异步功能时,实质是使用的 Servlet3 及以上版本的异步功能. Spring 的异步处理机制需要在 web.xml 中全部的 servlet 和 filter 处配置 < ...
- C#各版本新增加功能
本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...
- 【转】C#各版本新增加功能
本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...
- oracle_单向函数_数字化功能
oracle_单向函数_数字化功能 1.abs(x) 为了获得x绝对值 2.ceil(x) 用于获得大于或等于x的最小整数. 3.floor(x) 用于获得小于或等于x 4.mod(x,y ...
- python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学
首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...
- java web开发_购物车功能实现
java web开发_购物车功能实现 之前没有接触过购物车的东东,也不知道购物车应该怎么做,所以在查询了很多资料,总结一下购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: ...
- unix网络编程第2版(卷1)_第6章_同步_异步
第6章 I/O复用:select和poll函数 6.1概述 在5.12节中,我们看到TCP客户同时处理两个输入:标准输入和TCP套接口.我们遇到的问题是客户阻塞于(标准输入上的)fgets调用,而服务 ...
- 爬虫必知必会(4)_异步协程-selenium_模拟登陆
一.单线程+多任务异步协程(推荐) 协程:对象.可以把协程当做是一个特殊的函数.如果一个函数的定义被async关键字所修饰.该特殊的函数被调用后函数内部的程序语句不会被立即执行,而是会返回一个协程对象 ...
随机推荐
- C++ 凸包生成算法
由于我的极差记忆力,我打算把这个破玩意先记下来.因为以后会有改动(Delaunay三角网生成算法),我不想把一个好的东西改坏了... 好吧-- 凸包生成算法,: 1.先在指定的宽(width)高(he ...
- CentOS7之Rsync+Inotify架构实现实时同步文件和文件夹
简介:rsync是用来同步文件和文件夹的,inotify是用来实现监听变动而自动同步的 OS:Centos7.3 服务器端:172.16.13.157 客 户 端 :172.16.13.156 目 ...
- chrome添加离线插件
1.首先用户点击谷歌浏览器右上角的自定义及控制按钮,在下拉框中选择工具选项,然后点击扩展程序来启动Chrome浏览器的扩展管理器页面. 2.在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序 ...
- Django学习笔记之视图高级-类视图
类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图.使用类视图可以使用类的一些特性,比如继承等. View django.views.generic.base.View是主 ...
- Assets Library开发总结
Assets Library beta版的开发工作告一段落,本着有始有终的原则,这个项目还是需要做个总结的,恩~ 先甩一个链接:https://vimeo.com/238186671 考虑到该项目开发 ...
- html5课件外包-----swf/AS2/AS3/fla/ppt课件如何转换为html5交互课件/动画
随着Adobe公司公布2020年将不再更新和维护flash,flash逐渐被html5取代,很多教育机构都在面临着如何将自己的flash交互课件产品转换到html5版本的问题,最近遇到非常多的客户一上 ...
- soapui的简单使用
工具下载地址:https://www.soapui.org/downloads/soapui.html 名词解释 https://www.cnblogs.com/fcfblog/p/5830205.h ...
- Spring boot加载REACTIVE源码分析
一,加载REACTIVE相关自动配置 spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REA ...
- MySQL-exists和in的区别
SQL查询中in和exists的区别分析 对于一些不可不免的查询场景,我们难免要用到子查询 那么in和exists那个的效率更高一点呢 SQL查询中in和exists的区别分析 select * fr ...
- ubuntu 装机步骤表
步骤 1. root 步骤 apt-get update ; apt-get upgrate apt-get install git zsh apt-get install -y make build ...