最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React使用redux基礎(chǔ)操作詳解

 更新時(shí)間:2026年02月07日 09:24:31   作者:web老猴子  
這篇文章主要介紹了如何在React中直接使用Redux,目前redux在react中使用是最多的,所以我們需要將之前編寫的redux代碼,融入到react當(dāng)中去,本文給大家詳細(xì)講解,需要的朋友可以參考下

一,什么是redux

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) react-redux工作流程 安裝redux

npm install --save redux

簡(jiǎn)單使用

在src下新建store文件夾,創(chuàng)建倉(cāng)庫(kù)管理文件index.js

import { createStore, applyMiddleware, compose } from 'redux'  // 引入createStore方法
import reducer from "./reducer"
const store = createStore(reducer) // 創(chuàng)建數(shù)據(jù)存儲(chǔ)倉(cāng)庫(kù)
export default store                 //暴露出去 

同時(shí)創(chuàng)建reducer.js文件

//定義初始state
const defaultState = {
  inputValue: '請(qǐng)輸入待辦事項(xiàng)',
  list: [
    '早上4點(diǎn)起床,鍛煉身體',
    '中午下班游泳一小時(shí)']
}
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谷歌調(diào)試工具

下載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_()
                         ) // 創(chuàng)建數(shù)據(jù)存儲(chǔ)倉(cāng)庫(kù),存在調(diào)試工具,開(kāi)啟工具
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對(duì)象
    const action ={
  type:'changeInput',
      value:e.target.value
  }
    //調(diào)用dispatch
    store.dispatch(action)}
  // 訂閱更新
  storeChange() {
    this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action ={
  type:'addItem',
  }store.dispatch(action)}//點(diǎn)擊刪除事件deleteItem(index) {
    const action = {
  type:'deleteItem',
      index
  }
    store.dispatch(action)}
}
export default TodoList; 

在reducer.js中

//定義初始state
const defaultState = {
  inputValue: '請(qǐng)輸入待辦事項(xiàng)',
  list: [
    '早上4點(diǎn)起床,鍛煉身體',
    '中午下班游泳一小時(shí)']
}
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 = '' //增加完成,設(shè)置為空
    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對(duì)象
    #使用引入的常量替換
    const action ={
  type:CHANGE_INPUT,
      value:e.target.value
  }
    //調(diào)用dispatch
    store.dispatch(action)}
  // 訂閱更新
  storeChange() {
    this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action ={
  type:ADD_ITEM,
  }store.dispatch(action)}//點(diǎn)擊刪除事件deleteItem(index) {
    const action = {
  type:DELETE_ITEM,
      index
  }
    store.dispatch(action)}
}
export default TodoList; 

在reducer.js中也進(jìn)行引入

import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM } from './actionType'
//定義初始state
const defaultState = {
  inputValue: '請(qǐng)輸入待辦事項(xiàng)',
  list: [
    '早上4點(diǎn)起床,鍛煉身體',
    '中午下班游泳一小時(shí)']
}
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 = '' //增加完成,設(shè)置為空
    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派發(fā)

在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)
    //調(diào)用dispatch
    store.dispatch(action)}
  // 訂閱更新
  storeChange() {
    this.setState(store.getState())}// 添加按鈕事件clickBtn(){const action =addItemAction()store.dispatch(action)}//點(diǎn)擊刪除事件deleteItem(index) {
    const action = deleteItemAction(index)
    store.dispatch(action)}
}
export default TodoList; 

到此這篇關(guān)于React使用redux基礎(chǔ)操作詳解的文章就介紹到這了,更多相關(guān)React使用redux內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React中的?ref?及原理解析

    React中的?ref?及原理解析

    本章深入探討了React?Ref的用法和原理,還介紹了如何使用useImperativeHandle在函數(shù)組件中暴露方法,并詳細(xì)解釋了ref的處理邏輯和原理,包括在commit階段更新ref以及在組件卸載時(shí)的處理,感興趣的朋友一起看看吧
    2025-01-01
  • React commit源碼分析詳解

    React commit源碼分析詳解

    前兩章講到了,react 在 render 階段的 completeUnitWork 執(zhí)行完畢后,就執(zhí)行 commitRoot 進(jìn)入到了 commit 階段,本章將講解 commit 階段執(zhí)行過(guò)程源碼
    2022-11-11
  • React BootStrap用戶體驗(yàn)框架快速上手

    React BootStrap用戶體驗(yàn)框架快速上手

    這篇文章主要介紹了React BootStrap用戶體驗(yàn)框架快速上手的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-03-03
  • React利用props的children實(shí)現(xiàn)插槽功能

    React利用props的children實(shí)現(xiàn)插槽功能

    React中并沒(méi)有vue中的?slot?插槽概念?不過(guò)?可以通過(guò)props.children?實(shí)現(xiàn)類似功能,本文為大家整理了實(shí)現(xiàn)的具體方,需要的可以參考一下
    2023-07-07
  • 使用React.forwardRef傳遞泛型參數(shù)

    使用React.forwardRef傳遞泛型參數(shù)

    這篇文章主要介紹了使用React.forwardRef傳遞泛型參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解react native頁(yè)面間傳遞數(shù)據(jù)的幾種方式

    詳解react native頁(yè)面間傳遞數(shù)據(jù)的幾種方式

    這篇文章主要介紹了詳解react native頁(yè)面間傳遞數(shù)據(jù)的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • 詳解React中的todo-list

    詳解React中的todo-list

    這篇文章主要介紹了React中的todo-list的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • react 中父組件與子組件雙向綁定問(wèn)題

    react 中父組件與子組件雙向綁定問(wèn)題

    這篇文章主要介紹了react 中父組件與子組件雙向綁定問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • React中Ant?Design組件日期編輯回顯的實(shí)現(xiàn)

    React中Ant?Design組件日期編輯回顯的實(shí)現(xiàn)

    本文主要介紹了React中Ant?Design組件日期編輯回顯的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • 關(guān)于react的代理配置(可配置多個(gè)代理)

    關(guān)于react的代理配置(可配置多個(gè)代理)

    這篇文章主要介紹了關(guān)于react的代理配置(可配置多個(gè)代理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評(píng)論

梓潼县| 葫芦岛市| 仪征市| 吴川市| 贵港市| 亚东县| 江川县| 安化县| 梁平县| 赫章县| 长岭县| 彩票| 铁岭市| 裕民县| 平乐县| 林甸县| 曲麻莱县| 沾益县| 肥东县| 库伦旗| 兰州市| 井冈山市| 新巴尔虎右旗| 嘉义市| 于都县| 古交市| 高要市| 科尔| 蓝山县| 磴口县| 乐平市| 沾益县| 满城县| 汝阳县| 东乌珠穆沁旗| 西宁市| 连江县| 哈密市| 钟祥市| 鲁甸县| 乐都县|