網站首頁 編程語言 正文
一,什么是redux
Redux是一個用來管理管理數據狀態和UI狀態的JavaScript應用工具。隨著JavaScript單頁應用(SPA)開發日趨復雜,JavaScript需要管理比任何時候都要多的state(狀態),Redux就是降低管理難度的。(Redux支持React,Angular、jQuery甚至純JavaScript) react-redux工作流程 安裝redux
npm install --save redux
簡單使用
在src下新建store文件夾,創建倉庫管理文件index.js
import { createStore, applyMiddleware, compose } from 'redux' // 引入createStore方法
import reducer from "./reducer"
const store = createStore(reducer) // 創建數據存儲倉庫
export default store //暴露出去
同時創建reducer.js文件
//定義初始state
const defaultState = {
inputValue: '請輸入待辦事項',
list: [
'早上4點起床,鍛煉身體',
'中午下班游泳一小時']
}
export default (state = defaultState, action) => {
return state
}
組件中使用state的值
import React, { Component } from 'react';
//組件中引入store
import store from './store'
class TodoList extends Component {
constructor(props) {
super(props)
#獲取store中state的值
this.state = store.getState();
this.clickBtn = this.clickBtn.bind(this)
?}
render() {
return (
<div>
<Input placeholder={this.state.inputValue} style={{ width: '250px', marginRight: '10px' }} value={this.state.inputValue} />
<Button type="primary" onClick={clickBtn}>增加</Button>
</div>
<div style={{ margin: '10px', width: '300px' }}>
<List bordered
dataSource={this.state.list}
renderItem={(item, index) => (<List.Item onClick={() => {
deleteItem(index)
}}>{item}</List.Item>)}></List>
</div>
);}deleteItem(index) {console.log(index)}
}
export default TodoList;
二,安裝redux谷歌調試工具
翻墻下載redux_dev_tool, 在store/index文件下添加
import { createStore, applyMiddleware, compose } from 'redux' // 引入createStore方法
import reducer from "./reducer"
//const composeEnhancers =
//const enhancers = composeEnhancers(applyMiddleware(thunk))
const store = createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION_ && window.__REDUX_DEVTOOLS_EXTENSION_()
) // 創建數據存儲倉庫,存在調試工具,開啟工具
export default store
三,操作store 改變
import React, { Component } from 'react';
//組件中引入store
import store from './store'
class TodoList extends Component {
constructor(props) {
super(props)
#獲取store中state的值
this.state = store.getState();
this.clickBtn = this.clickBtn.bind(this)this.changeInputValue = this.changeInputValue.bind(this)
#添加訂閱 #新版本不用添加訂閱 但是input value變化需要使用訂閱
store.subscribe(this.storeChange)
this.storeChange = this.storeChange.bind(this)}
render() {
return (
<div>
<Input placeholder={this.state.inputValue} style={{ width: '250px', marginRight: '10px' }} onChange={()=>{this.changeInputValue} value={this.state.inputValue} />
<Button type="primary" onClick={this.clickBtn}>增加</Button>
</div>
<div style={{ margin: '10px', width: '300px' }}>
<List bordered
dataSource={this.state.list}
renderItem={(item, index) => (
<List.Item onClick={() => {deleteItem(index)}}>{item}</List.Item>)}></List>
</div>
);}changeInputValue(e){//聲明action對象
const action ={
type:'changeInput',
value:e.target.value
}
//調用dispatch
store.dispatch(action)}
// 訂閱更新
storeChange() {
this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action ={
type:'addItem',
}store.dispatch(action)}//點擊刪除事件deleteItem(index) {
const action = {
type:'deleteItem',
index
}
store.dispatch(action)}
}
export default TodoList;
在reducer.js中
//定義初始state
const defaultState = {
inputValue: '請輸入待辦事項',
list: [
'早上4點起床,鍛煉身體',
'中午下班游泳一小時']
}
export default (state = defaultState, action) => {
#reducer里只能接受state 不能改變state
if(action.type == 'changeInput'){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.inputValue = action.value
return newState}
//添加事件
if(action.type == 'addItem'){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.list.push(newState.inputValue)
newState.inputValue = '' //增加完成,設置為空
return newState}
//刪除事件
if(action.type == 'deleteItem'){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.list.splice(action.index,1)
return newState}
return state
}
四,寫redux的小技巧
在store中新建文件actionType.js
//定義常量
export const CHANGE_INPUT = 'changeInput'
export const ADD_ITEM = 'addItem'
export const DELETE_ITEM = 'deleteItem'
export const GET_LIST = 'getList'
在組件中引入actionType文件
import React, { Component } from 'react';
//組件中引入store
import store from './store'
import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM, GET_LIST } from './store/actionType'
class TodoList extends Component {
constructor(props) {
super(props)
#獲取store中state的值
this.state = store.getState();
this.clickBtn = this.clickBtn.bind(this)this.changeInputValue = this.changeInputValue.bind(this)
#添加訂閱 #新版本不用添加訂閱 但是input value變化需要使用訂閱
store.subscribe(this.storeChange)
this.storeChange = this.storeChange.bind(this)}
render() {
return (
<div>
<Input placeholder={this.state.inputValue} style={{ width: '250px', marginRight: '10px' }} onChange={()=>{this.changeInputValue} value={this.state.inputValue} />
<Button type="primary" onClick={this.clickBtn}>增加</Button>
</div>
<div style={{ margin: '10px', width: '300px' }}>
<List bordered
dataSource={this.state.list}
renderItem={(item, index) => (
<List.Item onClick={() => {deleteItem(index)}}>{item}</List.Item>)}></List>
</div>
);}changeInputValue(e){//聲明action對象
#使用引入的常量替換
const action ={
type:CHANGE_INPUT,
value:e.target.value
}
//調用dispatch
store.dispatch(action)}
// 訂閱更新
storeChange() {
this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action ={
type:ADD_ITEM,
}store.dispatch(action)}//點擊刪除事件deleteItem(index) {
const action = {
type:DELETE_ITEM,
index
}
store.dispatch(action)}
}
export default TodoList;
在reducer.js中也進行引入
import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM } from './actionType'
//定義初始state
const defaultState = {
inputValue: '請輸入待辦事項',
list: [
'早上4點起床,鍛煉身體',
'中午下班游泳一小時']
}
export default (state = defaultState, action) => {
#reducer里只能接受state 不能改變state
if(action.type == CHANGE_INPUT){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.inputValue = action.value
return newState}
//添加事件
if(action.type == ADD_ITEM){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.list.push(newState.inputValue)
newState.inputValue = '' //增加完成,設置為空
return newState}
//刪除事件
if(action.type == DELETE_ITEM){
let newState = JSON.pares(JSON.stringify(state)) //深拷貝state
newState.list.splice(action.index,1)
return newState}
return state
}
集中整理action派發
在store中新建actionCreator.js文件
import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM } from './actionType'
export const changeInputAction = (value) =>({type:CHANGE_INPUT,
value
})
export const addItemAction = () =>({type:ADD_ITEM,
})
export const deleteItemAction = (index) =>({type:DELETE_ITEM,
index
})
在組件中引入actionCreator.js
import React, { Component } from 'react';
//組件中引入store
import store from './store'
//引入actionCreator.js
import { changeInputAction,addItemAction,deleteItemAction } from './store/actionCreator'
class TodoList extends Component {
constructor(props) {
super(props)
#獲取store中state的值
this.state = store.getState();
this.clickBtn = this.clickBtn.bind(this)this.changeInputValue = this.changeInputValue.bind(this)
#添加訂閱 #新版本不用添加訂閱 但是input value變化需要使用訂閱
store.subscribe(this.storeChange)
this.storeChange = this.storeChange.bind(this)}
render() {
return (
<div>
<Input placeholder={this.state.inputValue} style={{ width: '250px', marginRight: '10px' }} onChange={()=>{this.changeInputValue} value={this.state.inputValue} />
<Button type="primary" onClick={this.clickBtn}>增加</Button>
</div>
<div style={{ margin: '10px', width: '300px' }}>
<List bordered
dataSource={this.state.list}
renderItem={(item, index) => (
<List.Item onClick={() => {deleteItem(index)}}>{item}</List.Item>)}></List>
</div>
);}changeInputValue(e){
const action = changeInputAction(e.target.value)
//調用dispatch
store.dispatch(action)}
// 訂閱更新
storeChange() {
this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action =addItemAction()store.dispatch(action)}//點擊刪除事件deleteItem(index) {
const action = deleteItemAction(index)
store.dispatch(action)}
}
export default TodoList;
原文鏈接:https://blog.csdn.net/web220507/article/details/128643708
相關推薦
- 2022-12-05 通過sc命令獲得System權限的代碼_DOS/BAT
- 2022-04-25 深入理解React?三大核心屬性_React
- 2022-07-23 C++深入細致探究二叉搜索樹_C 語言
- 2022-10-04 Nginx?502?bad?gateway錯誤解決的九種方案及原因_nginx
- 2022-09-30 docker部署golang?http服務時端口無法訪問的問題解決_docker
- 2022-12-28 詳解Flink同步Kafka數據到ClickHouse分布式表_數據庫其它
- 2022-06-08 ASP.NET?Core使用IHttpClientFactory發出HTTP請求_基礎應用
- 2022-07-29 C++超詳細講解數組操作符的重載_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支