一,项目目录

二、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练习项目的更多相关文章

  1. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)

    一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...

  2. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  3. Flux --> Redux --> Redux React 入门

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  4. React,关于redux的一点小见解

    最近项目做多页面应用使用到了,react + webpack + redux + antd去构建多页面的应用,本地开发用express去模拟服务端程序(个人觉得可以换成dva).所以在这里吐槽一下我自 ...

  5. 在React中使用Redux

    这是Webpack+React系列配置过程记录的第六篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...

  6. 新手级配置 react react-router4.0 redux fetch sass

    前言 最近公司来了几个实习生,刚好我手头没什么要紧事,然后领导让我带他们学习react, 为下一个react项目做基础. 然后随手写了几个demo,帮助他们了解正经项目如何去构建配置项目. 现在分享出 ...

  7. React 环境增加Redux ,React-Redux

    引入 Redux 的目的, 状态管理! React-Redux 就是完成一些粘合剂的作用. 简而化之的理解就是将数据放在store 中维护, 操作逻辑放在reducer中去写. 更功利的表达就是:  ...

  8. Flux --> Redux --> Redux React 基础实例教程

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  9. react系列(五)在React中使用Redux

    上一篇展示了Redux的基本使用,可以看到Redux非常简单易用,不限于React,也可以在Angular.Vue等框架中使用,只要需要Redux的设计思想的地方,就可以使用它. 这篇主要讲解在Rea ...

  10. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...

随机推荐

  1. mac 安装并使用 mysql 或者 mac mysql 忘记密码,Can't connect to local MySQL server through socket homebrew

    1. brew install mysql 2. 启动mysql mysql.server start 我遇到了这个error,查openstack解决,我在这粘一下 ### Error:Can't ...

  2. [转]C# 将类的内容写成JSON格式的字符串

    将类的内容写入到JSON格式的字符串中 本例中建立了Person类,赋值后将类中内容写入到字符串中 运行本代码需要添加引用动态库Newtonsoft.Json 程序代码: using System; ...

  3. class13and14and15_登录窗口

    最终的运行效果图(程序见序号6.2): #!/usr/bin/env python# -*- coding:utf-8 -*-# ----------------------------------- ...

  4. 剑指offer——27二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  5. java could not open `C|D|E|F:\jre\lib\amd64\jvm.cfg' 解决方案与原因

    因为安装了 jdk 后发现有多个 jre 一个是安装目录下的. 还有一个是安装后的自动安装的注意路径都不一样. 由于本人有强迫症所有不能容忍有两个 jre 目录的存在,所以果断删除了 D 盘下的.谨慎 ...

  6. 《DSP using MATLAB》Problem 8.42

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. Java中哪个JSON库的解析速度是最快的?

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上 ...

  8. cmd命令符

    运行操作 CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) CMD命令锦集       1. gpedit.msc-----组策略 2. ...

  9. EXCEL表格链接SQLSEVER数据库

    Sub 数据库连接()     Set Cnn = CreateObject("ADODB.Connection")    Set rs = CreateObject(" ...

  10. The linux command 之 扩展

    echo * " * "字符意味着匹配文件名中的任意字符,shell会在执行echo命令之前把*扩展成其他内容. 一.路径扩展(pathname Expansion) 通过使用通配 ...