一,项目目录

二、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. NX二次开发-将信息窗口中的文本保存到文本文件中UF_UI_save_listing_window

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //打开信息窗口 UF_UI_open_listing_window() ...

  2. NX二次开发-NXOPEN工程图导出CAD图纸DxfdwgCreator *dxfdwgCreator1;

    没有什么可以看的,NXOPEN直接录制一下导出CAD就可以了.录制出来自己挑需要的代码拿过来改一下. NX9+VS2012 #include <NXOpen/Part.hxx> #incl ...

  3. NXOpenC#_Training_blockstyler(cn)【转载】

  4. Debian怎么配置网卡(IP)

    来自:http://jingyan.baidu.com/article/a3f121e4d27a53fc9152bb65.html Debian可以配置静态IP.动态IP使Debian连上互联网.用户 ...

  5. 54Mbps、150Mbps、433Mbps 你知道这三个Wi-Fi速率怎么算的吗?

           802.11g能够提供54Mbps的最大速率, 802.11n和802.11ac单流分别能够提供150Mbps和433Mbps的最大速率,这些数字是怎么算的呢?(看红字,更容易理解哟) ...

  6. 【POJ】1321棋盘问题

    题目链接:http://poj.org/problem?id=1321 题意:见题干,很清楚了. 题解:简单dfs,参照八皇后 代码: #include<iostream> #includ ...

  7. 【python】collections的使用

    老师布置了一个课后作业. 统计文本中字母出现的次数并找到最大的那一个 首先是读取文本吧.和c里的也差不多,打开,关闭,读取. path = f = f.close() 然后就用到了这个黑科技.coll ...

  8. Codeforces 479【C】div3

    题目链接:http://codeforces.com/problemset/problem/977/C 题意:给你n个数字,输出任意一个数字,这个数字刚好大于等于,序列里面k个数字. 题解:排个序,第 ...

  9. Android开发 AndroidStudio解决Error:moudle not specified

    问题描述 在使用Android Studio 进行Builder APKs的时候,如果发现无法degub, 进行配置的时候 没有module可以进行指定 问题原因 项目未与Grade Files 文件 ...

  10. ES5给object扩展的一些静态方法

    1. Object.create(prototype[, descriptors]) : 创建一个新的对象 1). 以指定对象为原型创建新的对象 2). 指定新的属性, 并对属性进行描述 value ...