react+redux+react-redux练习项目
一,项目目录

二、1、新建pages包,在pages中新建TodoList.js;
2、新建store包,在store包中新建store.js,reducer.js,actionCreater.js,actionType.js;
3、在public包中新建list.json
三、安装redux,react-redux,axios
yarn add redux --save, yarn add react-redux --save,yarn add axios --save
四、index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import { Provider } from 'react-redux'
import TodoList from './pages/TodoList'
import store from './store/store' const App =(
<Provider store={store}>
<TodoList />
</Provider>
);
ReactDOM.render(App, document.getElementById('root')); // If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
使用react-redux。在react-redux的使用中,关注两个方法:Provider和connect
- Provider把store放到context里,所有的子元素可以直接取到store(<TodoList/>和其他所有添加在<Provider></Provider>中的组件),本质上 Provider 就是给 connect 提供 store 用的。
- connect 是一个高阶组件,接受一个组件 WrappedComponent 作为参数,负责链接组件,把给到redux里的数据放到组件的属性里。主要有两个作用:1. 负责接受一个组件,把state里的一些数据放进去,返回一个组件;2. 数据变化的时候,能够通知组件
五、store.js
import { createStore,applyMiddleware,compose} from 'redux';
import reducer from './reducer';
import thunk from 'redux-thunk';
//添加redux工具
const composeEnhancers= window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
const enhancerss = compose(
applyMiddleware(thunk),composeEnhancers
);
const store=createStore(reducer,enhancerss);
export default store;
引入redux-thunk中间件 他允许你的action可以返回函数和对象(不使用中间件只能返回对象), 带有dispatch和getState两个参数, 在这个action函数里, 异步的dispatch action;
六、reducer.js
const defaultState={
inputValue:'',
list:[]
}
export default (state=defaultState,action)=>{
switch(action.type){
case "change_input_value":
{
const newState =JSON.parse(JSON.stringify(state))
newState.inputValue=action.value;
return newState;
}
case "add_item":
{
const newState =JSON.parse(JSON.stringify(state))
newState.list.push(newState.inputValue)
return newState
}
case "init_list_action":
{
const newState=JSON.parse(JSON.stringify(state));
newState.list=action.data;
return newState;
}
case "delete_item":
{
const newState=JSON.parse(JSON.stringify(state));
newState.list.splice(action.index, 1)
return newState;
}
default:
return state
}
}
七、actionType.js
export const CHANGE_INPUT_VALUE='change_input_value'
export const ADD_ITEM='add_item'
export const INIT_LIST_ACTION='init_list_action'
export const DELETE_ITEM='delete_item'
八、actionCreater.js
import axios from 'axios'
import {CHANGE_INPUT_VALUE,ADD_ITEM,INIT_LIST_ACTION,DELETE_ITEM} from './actionType' export const initListAction = (data)=>({
type: INIT_LIST_ACTION,
data
}) export const deleteItem =(value)=>({
type:DELETE_ITEM,
value
})
export const additem =(value)=>({
type:ADD_ITEM,
value
}) export const changeValue =(value)=>({
type:CHANGE_INPUT_VALUE,
value
}) export const getTodoList=()=>{
return(dispatch)=>{
axios.get('./list.json').then((res)=>{
const data=res.data
const action =initListAction(data)
dispatch(action);
})
}
}
九、TodoList.js
import React from 'react';
import store from '../store/store'
import { connect} from 'react-redux'
import {initListAction,getTodoList,additem,changeValue,deleteItem} from '../store/actionCreater' class TodoList extends React.Component{
constructor(){
super();
this.state=store.getState();
} componentDidMount(){
const action=getTodoList();
store.dispatch(action);
}
render(){
const {list,inputValue,changeInputValue,handleAddClick,handleDelete} = this.props
return(
<div>
<div>
<input value={inputValue} onChange={changeInputValue}></input>
<button onClick={handleAddClick}>提交</button>
</div>
<ul>
<li>
{
list &&list.map((item,index)=>{
return <li onClick={()=>{handleDelete(index)}} key={index}>{item}</li>
})
}
</li>
</ul>
</div>
)
}
} const mapStateToProps =(state)=>{
return {
inputValue:state.inputValue,
list:state.list
}
} const mapDispatchToProps =(dispatch)=>{
return{
changeInputValue(e){
const action=changeValue(e.target.value)
dispatch(action)
},
//新增数据
handleAddClick(){
const action =additem()
dispatch(action)
},
//删除数据
handleDelete(index){
const action=deleteItem(index)
dispatch(action)
}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(TodoList);//和store做连接
十、list.json
["hello","nihao","haha"]
react+redux+react-redux练习项目的更多相关文章
- 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)
一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...
- RxJS + Redux + React = Amazing!(译一)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...
- Flux --> Redux --> Redux React 入门
本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...
- React,关于redux的一点小见解
最近项目做多页面应用使用到了,react + webpack + redux + antd去构建多页面的应用,本地开发用express去模拟服务端程序(个人觉得可以换成dva).所以在这里吐槽一下我自 ...
- 在React中使用Redux
这是Webpack+React系列配置过程记录的第六篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...
- 新手级配置 react react-router4.0 redux fetch sass
前言 最近公司来了几个实习生,刚好我手头没什么要紧事,然后领导让我带他们学习react, 为下一个react项目做基础. 然后随手写了几个demo,帮助他们了解正经项目如何去构建配置项目. 现在分享出 ...
- React 环境增加Redux ,React-Redux
引入 Redux 的目的, 状态管理! React-Redux 就是完成一些粘合剂的作用. 简而化之的理解就是将数据放在store 中维护, 操作逻辑放在reducer中去写. 更功利的表达就是: ...
- Flux --> Redux --> Redux React 基础实例教程
本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...
- react系列(五)在React中使用Redux
上一篇展示了Redux的基本使用,可以看到Redux非常简单易用,不限于React,也可以在Angular.Vue等框架中使用,只要需要Redux的设计思想的地方,就可以使用它. 这篇主要讲解在Rea ...
- React学习之redux
在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...
随机推荐
- fedora23上安装和运行MySQL server (MySQL 已经被MariaDB取代)
[root@localhost kemin]# dnf install mysql-server Fedora 23 - x86_64 - Updates ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- 一幅图解决R语言绘制图例的各种问题
一幅图解决R语言绘制图例的各种问题 用R语言画图的小伙伴们有木有这样的感受,"命令写的很完整,运行没有报错,可图例藏哪去了?""图画的很美,怎么总是图例不协调?" ...
- java 8 lambda函数
1 为什么要引进lambda函数 可以简化编码,将事情更多的交给编译器,让编译器帮我们推断我们写的代码的完整形式. 2 lambda函数的语法 2.1 -> (arg1, arg2) -> ...
- Parallels Desktop Centos 设置IP
参考链接 Parallels Desktop虚拟的Centos系统设置静态IP连网 https://blog.csdn.net/hotdust/article/details/53812953#com ...
- Crane /// 向量旋转+线段树
题目大意: 给定n条首尾相接的线段的长度 第一条从0,0开始,所有线段垂直与x轴向上延伸 给定c次操作 每次操作给定 s,a 使得 由第s条线段的角度 逆时针旋转a后 达到第s+1条线段的角度 每次操 ...
- 整理下webapi的一些琐碎事情
在使用webapi的时候我们会遇到一些问题比如 1.POST怎么请求 2.怎么兼容JSONP请求 3.怎么给指定端提供跨域的请求 4.怎么显示单独的models层的注释 问题一二其他人都玩的比较成熟的 ...
- AOP与IOC的概念(即spring的核心)
a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而spring的核心是IOC控制反转和AOP面向切面 ...
- python_django_models模块
django中models模块为各类数据库提供了统一的api,可根据不同的业务需求配置数据库. models模块开发流程: 配置数据库 详情:https://www.cnblogs.com/Vera ...
- Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解
基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...