一个redux使用案例模板
创始人
2024-03-24 15:23:02
0

目录

redux

纯函数和高阶函数:

 redux 开发工具使用

react-redux


redux

1. 结构:

 count--index.jsx

import React, { Component } from 'react'
import store from '../../redux/store'
import { acDecrement,acIncrement,acAsyncIncrement } from '../../redux/count_action'export default class Count extends Component {state = {count:0}// store 里面的数据法神变化,更新页面componentDidMount(){store.subscribe(()=>{this.setState({})})}increment = ()=>{let value = this.selectRef.valuestore.dispatch(acIncrement(value*1))}decrement = ()=>{let value = this.selectRef.valuestore.dispatch(acDecrement(value*1))}handleOdd = ()=>{let value = this.selectRef.valueif(store.getState() % 2 !== 0) store.dispatch(acIncrement(value*1))}handleAsync = ()=>{let value = this.selectRef.value// let count = this.state.count// setTimeout(()=>{//   store.dispatch(acIncrement(value*1))// },500)store.dispatch(acAsyncIncrement(value*1,500))}render() {// console.log(this)return (

当前求和{store.getState()}

)} }

redux--store.js

import {legacy_createStore as createStore,applyMiddleware} from 'redux'
// 创建store需要引入为redux服务的reducer
import reducer from './count_reducer'// 引入 异步action 需要的中间件 thunk ,通过applyMiddleware使用此中间件
import thunk from 'redux-thunk'//暴露store
export default createStore(reducer,applyMiddleware(thunk))

redux--reducer.js

// 就是一个函数
// 接收两个参数: preState 和 actionlet initCount = 5
export default function countReducer(preState=initCount,action) {// console.log(preState,'preState')let {data,type} = actionswitch(type) {case 'increment': return preState+datacase 'decrement': return preState-datadefault: return preState}
}

redux--action.js

// 就是个函数
// 同步action 返回对象,异步action 返回对象// 异步action 需要中间件 redux-thunkexport const acIncrement = data=>({type:'increment',data})
export const acDecrement = data=>({type:'decrement',data})
export const acAsyncIncrement = (data,time)=>{return (dispatch)=>{setTimeout(()=>{dispatch(acIncrement(data))},time)}
}

纯函数和高阶函数:

 redux 开发工具使用

在谷歌中导入文件夹(需要下载)

react-redux

 1. 结构:

containers--count.jsx

import React, { Component } from 'react'
import { connect } from "react-redux";
import {acDecrement,acAsyncIncrement,acIncrement} from '../../redux/count_action'class CountUI extends Component {state = {count:0}// store 里面的数据法神变化,更新页面// componentDidMount(){//   store.subscribe(()=>{//     this.setState({})//   })// }increment = ()=>{let value = this.selectRef.valuethis.props.PropIncrement(value*1)}decrement = ()=>{let value = this.selectRef.valuethis.props.PropDecrement(value*1)}handleOdd = ()=>{let value = this.selectRef.valueif(this.props.count % 2 !==0) {this.props.PropIncrement(value*1)}}handleAsync = ()=>{let value = this.selectRef.valuethis.props.PropAsyncIncrement(value*1,500)}render() {// console.log(this)return (

当前求和{this.props.count}

)} }export default connect((state)=>({count:state.reducerCount}),// api 优化{PropIncrement:acIncrement,PropDecrement:acDecrement,PropAsyncIncrement: acAsyncIncrement,}/* (dispatch)=>({PropIncrement: (data) => dispatch(acIncrement(data)),PropDecrement: (data) => dispatch(acDecrement(data)),PropAsyncIncrement: (data,time) => dispatch(acAsyncIncrement(data,time)),}) */ )(CountUI)

 ------person.jsx

import React, { Component } from 'react'
// import store from '../../redux/store'// 使用 store 里面的共享数据
import { connect } from 'react-redux'class PersonUI extends Component {render() {return (// 
PPindex{store.getState().reducerCount}
PPindex{this.props.reducerCount}
)} }// 自动检测store 里面的数据发生变化,然后自动更新页面 export default connect((state)=>({reducerCount: state.reducerCount}),{} )(PersonUI)

 redux---reducer.js和action.js  (不变)

// 就是一个函数
// 接收两个参数: preState 和 actionlet initCount = 5
export default function countReducer(preState=initCount,action) {// console.log(preState,'preState')let {data,type} = actionswitch(type) {case 'increment': return preState+datacase 'decrement': return preState-datadefault: return preState}
}//action
// 就是个函数
// 同步action 返回对象,异步action 返回对象// 异步action 需要中间件 redux-thunkexport const acIncrement = data=>({type:'increment',data})
export const acDecrement = data=>({type:'decrement',data})
export const acAsyncIncrement = (data,time)=>{return (dispatch)=>{setTimeout(()=>{dispatch(acIncrement(data))},time)}
}

------store.js

import {legacy_createStore as createStore,applyMiddleware, combineReducers} from 'redux'
// 创建store需要引入为redux服务的reducer
import reducerCount from './count_reducer'// 引入 异步action 需要的中间件 thunk ,通过applyMiddleware使用此中间件
import thunk from 'redux-thunk'//合并reudcer 需要 combineReducers
//汇总最好直接写在 reducer 文件夹里面的index.js中
const allReducer = combineReducers({reducerCount: reducerCount,
})
//暴露store
export default createStore(allReducer,applyMiddleware(thunk))

App.jsx

import React, { Component } from 'react'
import Count from './containers/count'
import Person from './containers/person'export default class App extends Component {render() {return (
)} }

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from './App'
import store from './redux/store'
import {Provider} from 'react-redux'ReactDOM.render(,document.getElementById('root')
) // 用react-redux 不用再手动检测
// store.subscribe(()=>{
//   ReactDOM.render(,document.getElementById('root')) 
// })

相关内容

热门资讯

无惧苹果起诉,线人放出iPho... 据科技媒体 Phone Arena 昨天报道,在今年 7 月被苹果起诉后,爆料人 Jon Pross...
民间借贷纠纷:选靠谱律师,彭艳... 在民间借贷纠纷频发的当下,寻找一位专业、靠谱且性价比高的律师至关重要。民间借贷纠纷涉及的法律问题复杂...
大烨智能刚收到立案告知书,律师... 雷达财经雷助吧出品 文|阑珊 编|深海 12月26日,大烨智能发布《关于收到中国证券监督管理委员会立...
推荐靠谱高性价比离婚纠纷律师:... 在人生的旅途中,离婚纠纷往往是一个令人痛苦且棘手的难题。当面临离婚时,涉及财产分割、子女抚养权争夺等...
电芯被指存在质量问题!吉利子公... 12月26日,欣旺达发布公告,公司子公司欣旺达动力于2025年12月25日收到浙江省宁波市中级人民法...
吉利旗下公司起诉欣旺达:“交付... 截至目前,国内罕有因交付电芯出现质量问题而和客户对簿公堂的动力电池制造商。老牌锂电企业欣旺达电子股份...
八位专家点评2025年劳动法律... 2025年劳动法律监督 “一函两书”典型案例专家点评 12月25日,中华全国总工会联合最高人民法院、...
全国人大常委会:对104件法律... 全国人民代表大会常务委员会关于批准《全国人民代表大会常务委员会法制工作委员会关于法律清理工作情况和处...
全国人民代表大会常务委员会关于... 新华社北京12月27日电 全国人民代表大会常务委员会关于《中华人民共和国刑事诉讼法》第二百九十二条的...