react用Redux中央倉(cāng)庫(kù)實(shí)現(xiàn)一個(gè)todolist
本文實(shí)例為大家分享了react用Redux中央倉(cāng)庫(kù)實(shí)現(xiàn)一個(gè)todolist的具體代碼,供大家參考,具體內(nèi)容如下
Redux簡(jiǎn)單介紹
Redux是一個(gè)用來(lái)管理管理數(shù)據(jù)狀態(tài)和UI狀態(tài)的JavaScript應(yīng)用工具。隨著JavaScript單頁(yè)應(yīng)用(SPA)開(kāi)發(fā)日趨復(fù)雜,JavaScript需要管理比任何時(shí)候都要多的state(狀態(tài)),Redux就是降低管理難度的。(Redux支持React,Angular、jQuery甚至純JavaScript)
Redux Dev Tools插件 Redux調(diào)試工具 谷歌商店下載
redux三個(gè)坑:
store倉(cāng)庫(kù)必須是唯一的,多個(gè)store是堅(jiān)決不允許,只能有一個(gè)store空間
只有store能改變自己的內(nèi)容,Reducer不能改變
Reducer必須是純函數(shù)
Redux-thunk這個(gè)Redux最常用的插件:
在Dispatch一個(gè)Action之后,到達(dá)reducer之前,進(jìn)行一些額外的操作,就需要用到middleware(中間件)
在實(shí)際工作中你可以使用中間件來(lái)進(jìn)行日志記錄、創(chuàng)建崩潰報(bào)告,調(diào)用異步接口或者路由
npm install --save redux-thunk
第一步 倉(cāng)庫(kù) 在store文件夾下新建index.js
//applyMiddleware,compose是為了使用下面兩個(gè)插件 import {createStore,applyMiddleware,compose} from 'redux' //引入redux import thunk from 'redux-thunk' //引入redux中間件插件 import reducer from './reducer' //引用reducer中的數(shù)據(jù) //瀏覽器安裝的倉(cāng)庫(kù)插件 調(diào)試面板 const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}):compose //redux中間件插件 此函數(shù)為了兼容兩個(gè)插件并行 const enhancer = composeEnhancers(applyMiddleware(thunk)) //定義一個(gè)倉(cāng)庫(kù) 唯一的 不能有兩個(gè)倉(cāng)庫(kù) createStore倉(cāng)庫(kù)只接收兩個(gè)參數(shù) const store = createStore( reducer, enhancer) // 創(chuàng)建數(shù)據(jù)存儲(chǔ)倉(cāng)庫(kù) export default store //將倉(cāng)庫(kù)導(dǎo)出
新建reducer.js 做倉(cāng)庫(kù)數(shù)據(jù)處理
import {ADD_ITEM , DELETE_ITEM , GET_LIST} from './actionTypes' //定義type類型的js文件 const defaultState = { value:'sss', list:[] //后端獲取的列表數(shù)據(jù)放在這里 } // state: 指的是原始倉(cāng)庫(kù)里的狀態(tài)。 // action: 指的是action新傳遞的狀態(tài)。 export default (state = defaultState,action)=>{ // console.log(state) //Reducer里只能接收state 不能改變state // if(action.type ==="changeInput"){ // let newState = JSON.parse(JSON.stringify(state)) //深拷貝state的值 轉(zhuǎn)成字符串 賦值給一個(gè)變量 // newState.value = action.vlaue //改變placeholder的值等于用戶輸入的值 // return newState //將新state return出去 // } //增加 if(action.type === ADD_ITEM ){ //根據(jù)type值,編寫業(yè)務(wù)邏輯 let newState = JSON.parse(JSON.stringify(state)) newState.list.push(action.value) //用戶輸入的新內(nèi)容push新的內(nèi)容到列表中去 console.log(action) newState.value = '' //增加后清空 return newState } //刪除 if(action.type === DELETE_ITEM ){ let newState = JSON.parse(JSON.stringify(state)) newState.list.splice(action.index,1) //刪除數(shù)組中對(duì)應(yīng)的值 return newState } //后端獲取數(shù)據(jù) 傳遞給中央倉(cāng)庫(kù)做處理 if(action.type === GET_LIST ){ let newState = JSON.parse(JSON.stringify(state)) newState.list =action.data return newState } return state }
actionTypes.js 集中管理頁(yè)面reducer的type類型
//集中管理頁(yè)面reducer的type類型 export const ADD_ITEM = "addItem" //定義常量一般用大寫 export const DELETE_ITEM = "deleteItem" export const GET_LIST = "getListAction"
actionCreators.js 封裝組件的action
//封裝組件的action import {ADD_ITEM , DELETE_ITEM ,GET_LIST} from './actionTypes' //定義type類型的js import axios from 'axios' //組件addItem里的action type已經(jīng)封好 所以把value作為參數(shù)用箭頭函數(shù)(value)=>傳進(jìn)來(lái)即可 //增加數(shù)據(jù) export const addItem = (value)=>({ type:ADD_ITEM, value }) //刪除數(shù)據(jù) export const deleteItem = (index)=>({ type:DELETE_ITEM, index }) //獲取數(shù)據(jù) export const getListAction = (data)=>({ type:GET_LIST, data }) export const getTodoList = () =>{ return (dispatch)=>{ axios.get('https://www.easy-mock.com/mock/5d63d7ca5774121e1a634378/demo1/demo1') .then((res)=> { const data = res.data.data; const action = getListAction(data) dispatch(action) //將action這個(gè)值傳給倉(cāng)庫(kù) }) .catch((error)=>{ console.log(error) }) } }
TodoList.js 組件js部分
import React, { Component } from 'react'; import TodoListUi from './TodoListUi' //組件UI部分 import store from '../store/index' //組件中獲得store中的數(shù)據(jù) //import {ADD_ITEM , DELETE_ITEM} from '../store/actionTypes' //定義type類型的js 為了更方便檢查錯(cuò)誤 寫錯(cuò)會(huì)報(bào)錯(cuò) import { addItem,deleteItem,getTodoList } from '../store/actionCreators' //封裝的action //用reducer寫todolist 調(diào)用中央倉(cāng)庫(kù) class TodoList extends Component { constructor(props){ super(props) // console.log(store.getState()) //getState方法取出倉(cāng)庫(kù)的值 this.state = store.getState() //將組件state數(shù)據(jù)賦值給倉(cāng)庫(kù)數(shù)據(jù) this.changeInputVlaue = this.changeInputVlaue.bind(this) //給方法做this綁定 this.storeChange = this.storeChange.bind(this) this.clickBtn = this.clickBtn.bind(this) this.deleteItem = this.deleteItem.bind(this) store.subscribe(this.storeChange) //訂閱模式 改變數(shù)據(jù)時(shí)同步讓倉(cāng)庫(kù)中的數(shù)據(jù)改變 } render() { return ( <TodoListUi value={this.state.value} changeInputVlaue={this.changeInputVlaue} clickBtn={this.clickBtn} list={this.state.list} deleteItem = {this.deleteItem} ></TodoListUi> ); } componentDidMount(){ // axios.get('https://www.easy-mock.com/mock/5d63d7ca5774121e1a634378/demo1/demo1') // .then((res)=> // { // const data = res.data.data; // const action =getListAction(data) //將取到的數(shù)據(jù)封入action // store.dispatch(action) //傳遞給reducer // }) // .catch((error)=>{ // console.log(error) // }) const action = getTodoList() //使用中間件獲取數(shù)據(jù) store.dispatch(action) //傳給倉(cāng)庫(kù) } //用戶輸入的值傳給倉(cāng)庫(kù) 要通過(guò)dispatch()方法傳遞給store //Action就是一個(gè)對(duì)象,這個(gè)對(duì)象一般有兩個(gè)屬性,第一個(gè)是對(duì)Action的描述,第二個(gè)是要改變的值。 //之前注銷的方法,在reducer里深拷貝完state里面的數(shù)據(jù),無(wú)法同步將用戶輸入賦值給state changeInputVlaue(e){ this.setState({ value:e.target.value //將用戶輸入的value綁定給倉(cāng)庫(kù)中的value,監(jiān)聽(tīng)用戶輸入 }) // const action = { // type:'changeInput', // 名字 // value:e.target.value //值 // } // store.dispatch(action) } //state和組件的值同步互相改變 storeChange(){ this.setState(store.getState()) } //增加 因?yàn)閘ist數(shù)據(jù)存在中央倉(cāng)庫(kù)里 所以要做的是 將組件數(shù)據(jù)傳給倉(cāng)庫(kù) 改變倉(cāng)庫(kù)數(shù)據(jù)后 再返回給組件展示 clickBtn(){ // console.log(this.state.value) // const action = { // type:ADD_ITEM, // value:this.state.value //獲取到用戶value,用于push // } const action = addItem(this.state.value); store.dispatch(action) } //刪除 deleteItem(index){ // console.log(index) // const action = { // type:DELETE_ITEM, // index //index傳過(guò)去用于刪除 // } const action =deleteItem(index) store.dispatch(action) } } export default TodoList;
TodoListUi.js 組件UI部分抽離成子組件
//此文件用于視圖和邏輯的分離 import React from 'react'; import 'antd/dist/antd.css' //引入Ant Design UI庫(kù) import { Input ,Button,List} from 'antd' //引入input組件 //無(wú)狀態(tài)組件 提高性能 將組件改造成函數(shù) const TodoListUi = (props)=>{ return ( <div style={{margin:"100px"}}> <div> <Input style={{ width:"250px",marginRight:"20px"}} onChange={props.changeInputVlaue} value={props.value} /> <Button type='primary' onClick={props.clickBtn}>增加</Button> </div> <div style={{margin:"10px",width:"300px"}}> <List bordered //加邊框 dataSource={props.list} //渲染什么數(shù)據(jù) renderItem={(item,index)=>(<List.Item onClick={()=>{props.deleteItem(index)}}>{item}</List.Item>)} //每項(xiàng) /> </div> </div> ); } //改造前組件 上邊需要從react引入Component // class TodoListUi extends Component { // state = { } // render() { // return ( // <div style={{margin:"100px"}}> // <div> // <Input // style={{ width:"250px",marginRight:"20px"}} // onChange={this.props.changeInputVlaue} // value={this.props.value} // /> // <Button type='primary' onClick={this.props.clickBtn}>增加</Button> // </div> // <div style={{margin:"10px",width:"300px"}}> // <List // bordered //加邊框 // dataSource={this.props.list} //渲染什么數(shù)據(jù) // renderItem={(item,index)=>(<List.Item onClick={()=>{this.props.deleteItem(index)}}>{item}</List.Item>)} //每項(xiàng) // /> // </div> // </div> // ); // } // } export default TodoListUi;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定
這篇文章主要介紹了React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03React進(jìn)階學(xué)習(xí)之組件的解耦之道
這篇文章主要給大家介紹了關(guān)于React進(jìn)階之組件的解耦之道,文中通過(guò)詳細(xì)的示例代碼給大家介紹了組件分割與解耦的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08React Fiber樹的構(gòu)建和替換過(guò)程講解
React Fiber樹的創(chuàng)建和替換過(guò)程運(yùn)用了雙緩存技術(shù),直接將舊的 fiber 樹替換成新的 fiber 樹,這樣做的好處是省去了直接在頁(yè)面上渲染時(shí)的計(jì)算時(shí)間,避免計(jì)算量大導(dǎo)致的白屏、卡頓,現(xiàn)在你一定還不太理解,下面進(jìn)行詳細(xì)講解,需要的朋友可以參考下2022-12-12關(guān)于React中setState同步或異步問(wèn)題的理解
相信很多小伙伴們都一直在疑惑,setState 到底是同步還是異步。本文就詳細(xì)的介紹一下React中setState同步或異步問(wèn)題,感興趣的可以了解一下2021-11-11React如何使用axios請(qǐng)求數(shù)據(jù)并把數(shù)據(jù)渲染到組件
這篇文章主要介紹了React如何使用axios請(qǐng)求數(shù)據(jù)并把數(shù)據(jù)渲染到組件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08React實(shí)現(xiàn)歌詞滾動(dòng)效果(跟隨音樂(lè)播放時(shí)間滾動(dòng))
這篇文章主要為大家詳細(xì)介紹了React實(shí)現(xiàn)歌詞滾動(dòng)效果(跟隨音樂(lè)播放使勁按滾動(dòng)),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2024-02-02react實(shí)現(xiàn)移動(dòng)端下拉菜單的示例代碼
這篇文章主要介紹了react實(shí)現(xiàn)移動(dòng)端下拉菜單的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01