日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

七十九、TodoList示例 深入Redux的工作流

發(fā)布時間:2024/10/8 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 七十九、TodoList示例 深入Redux的工作流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2020/11/20、 周五、今天又是奮斗的一天。

@Author:Runsen

React,也有了自己去構(gòu)建一些應用的信心,那會是一種非常棒的感覺。你學會了管理狀態(tài),一切看起來井井有條。但是,很有可能這就到了你該學習 Redux 的時候了。

文章目錄

  • Redux-devtools插件
  • Redux
  • ActionTypes和ActionCreator拆分

Redux-devtools插件

Redux DevTools是一個開源項目,是為谷歌瀏覽器用戶打造的一款實用調(diào)試插件,主要適用于開發(fā)者使用,使用該插件可以有效地對應用程序或者網(wǎng)頁的狀態(tài)進行調(diào)試操作。

我上傳到CSDN,下載鏈接:https://download.csdn.net/download/weixin_44510615/13130629

因為我是window系統(tǒng), 需要添加window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

Redux

原理:React首先改變store里面的數(shù)據(jù)state,先要開發(fā)一個Aciton,然后通過dispath方法傳遞給store,store再把之前的數(shù)據(jù)和Action轉(zhuǎn)化的數(shù)據(jù)在Reducers進行判斷和對比,Reducers本身就是一個函數(shù),返回新的state,store就替換之前的state。

下面舉添加點擊的Redux的工作流流程。

具體代碼

TodoList.js

import React, {Component } from 'react' import 'antd/dist/antd.css' import { Input,Button, List } from 'antd'; // import store from './store'相當于導入store/index,js import store from './store';class TodoList extends Component {constructor(props) {super(props)this.state = store.getState()this.handleInputChange = this.handleInputChange.bind(this)this.handleStoreChange = this.handleStoreChange.bind(this)this.handleBtnClick = this.handleBtnClick.bind(this)store.subscribe(this.handleStoreChange);console.log( this.state)}render() {return(<div><div><Input value={this.state.inputValue}placeholder="Todo info" style={{width:'300px',marginRight:'10px' }}onChange={this.handleInputChange}></Input><Button type="primary"onClick={this.handleBtnClick}>提交</Button></div><Listborderedstyle={{width:'300px',marginTop:'10px' }}dataSource={this.state.list}renderItem={(item,index) => (<List.Item onClick={this.handleItemDelete.bind(this,index)}>{item}</List.Item>)}/></div>)}handleInputChange(e) {const action = {type: 'change_input_value',value: e.target.value}store.dispatch(action)}handleStoreChange() {this.setState(store.getState());}handleBtnClick() {const action = {type: 'add_todo_item',}store.dispatch(action)}handleItemDelete(index) {const action = {type: 'delete_todo_item',index}store.dispatch(action)} }export default TodoList;

reducer.js

/* eslint-disable import/no-anonymous-default-export */ const defaultState = {inputValue : '123',list: [1,2]}export default (state = defaultState , action) => {if (action.type === 'change_input_value'){const newState = JSON.parse(JSON.stringify(state));newState.inputValue = action.valuereturn newState}if (action.type === 'add_todo_item'){const newState = JSON.parse(JSON.stringify(state));newState.list.push(newState.inputValue)newState.inputValue ='';return newState}if (action.type === 'delete_todo_item'){const newState = JSON.parse(JSON.stringify(state));newState.list.splice(action.inbdex,1)return newState}return state; }

index.js

import { createStore } from 'redux'; import reducer from './reducer';const store = createStore(reducer,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());export default store;

ActionTypes和ActionCreator拆分

由于全部代碼在TodoList.js寫死了,不容易維護,需要進行ActionTypes和ActionCreator拆分

具體目錄如下

TodoList.js

import React, {Component } from 'react' import 'antd/dist/antd.css' import { Input,Button, List } from 'antd'; // import store from './store'相當于導入store/index,js import store from './store'; import { getInitList, getInputChangeAction, getAddItemAction, getDeleteItemAction } from './store/actionCreators'class TodoList extends Component {constructor(props) {super(props)this.state = store.getState()this.handleInputChange = this.handleInputChange.bind(this)this.handleStoreChange = this.handleStoreChange.bind(this)this.handleBtnClick = this.handleBtnClick.bind(this)store.subscribe(this.handleStoreChange);console.log( this.state)}render() {return(<div><div><Input value={this.state.inputValue}placeholder="Todo info" style={{width:'300px',marginRight:'10px' }}onChange={this.handleInputChange}></Input><Button type="primary"onClick={this.handleBtnClick}>提交</Button></div><Listborderedstyle={{width:'300px',marginTop:'10px' }}dataSource={this.state.list}renderItem={(item,index) => (<List.Item onClick={this.handleItemDelete.bind(this,index)}>{item}</List.Item>)}/></div>)}componentDidMount() {const action = getInitList();store.dispatch(action);}handleInputChange(e) {const action = getInputChangeAction(e.target.value);store.dispatch(action);}handleStoreChange() {this.setState(store.getState());}handleBtnClick() {const action = getAddItemAction();store.dispatch(action);}handleItemDelete(index) {const action = getDeleteItemAction(index);store.dispatch(action);} }export default TodoList;

actionCreators.js

import { GET_INIT_LIST, CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM, INIT_LIST_ACTION } from './actionTypes';export const getInputChangeAction = (value) => ({type: CHANGE_INPUT_VALUE,value });export const getAddItemAction = () => ({type: ADD_TODO_ITEM });export const getDeleteItemAction = (index) => ({type: DELETE_TODO_ITEM,index });export const initListAction = (data) => ({type: INIT_LIST_ACTION,data });export const getInitList = () => ({type: GET_INIT_LIST });

actionTypes.js

export const CHANGE_INPUT_VALUE = 'change_input_value'; export const ADD_TODO_ITEM = 'add_todo_item'; export const DELETE_TODO_ITEM = 'delete_todo_item'; export const INIT_LIST_ACTION = 'init_list_action'; export const GET_INIT_LIST = 'get_init_list';

reducer.js

import { INIT_LIST_ACTION, CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM } from './actionTypes'const defaultState = {inputValue: '',list: [] }// reducer 可以接受state,但是絕不能修改state // 純函數(shù)指的是,給定固定的輸入,就一定會有固定的輸出,而且不會有任何副作用 export default (state = defaultState, action) => {if (action.type === CHANGE_INPUT_VALUE) {const newState = JSON.parse(JSON.stringify(state));newState.inputValue = action.value;return newState;}if (action.type === INIT_LIST_ACTION) {const newState = JSON.parse(JSON.stringify(state));newState.list = action.data;return newState;}if (action.type === ADD_TODO_ITEM) {const newState = JSON.parse(JSON.stringify(state));newState.list.push(newState.inputValue);newState.inputValue = '';return newState;}if (action.type === DELETE_TODO_ITEM) {const newState = JSON.parse(JSON.stringify(state));newState.list.splice(action.index, 1);return newState;}return state; }

總結(jié)

以上是生活随笔為你收集整理的七十九、TodoList示例 深入Redux的工作流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。