網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、react-redux
react-redux依賴于redux工作。 運(yùn)行安裝命令:npm i react-redux
:
使用: 將Provider套在入口組件處,并且將自己的store傳進(jìn)去:
import FilmRouter from './FilmRouter/index' import {Provider} from 'react-redux' import store from './FilmRouter/views/redux/store'; ReactDOM.render( <Provider store={store}> <FilmRouter /> </Provider> , document.getElementById('root') );
然后在子組件導(dǎo)出的時(shí)候套一層connet組件,并且把父組件需要傳入的值傳入:
import React, { Component } from 'react' import FRouter from './Router/ReactRouter' import Tabbar from './components/Tabbar' import { connect } from 'react-redux' // import store from './views/redux/store' // let unSubscribe class Index extends Component { state = { // list: store.getState().TabbarReducer.list } // store.subscribe 訂閱 componentDidMount() { console.log(this.props) // store.subscribe(() => { // console.log('訂閱', store.getState()) // this.setState({ // isShow: store.getState().TabbarReducer.flag // }) // }) // unSubscribe = store.subscribe(() => { // this.setState({ // list: store.getState().TabbarReducer.list // }) // }) } // componentWillUnmount() { // unSubscribe() // } render() { return ( <div> <FRouter> {this.props.isShow && <Tabbar></Tabbar>} </FRouter> </div> ) } } const mapStateToProps = (state) => { return { a: 1, b: 2, isShow: state.TabbarReducer.flag } } export default connect(mapStateToProps)(Index)
可以看到我們包了一層connect后把我們的reducer給傳入進(jìn)去,那在子組件中就可以直接使用this.props來獲取這個(gè)值來決定是否渲染了:
可以看到效果還是跟之前一樣,只不過采用了react-redux,那么我們?cè)贒etaile組件中就不用自己去分發(fā)dispatch事件了,將Details
組件修改為:
import React, {useEffect} from 'react' import { hide, show } from './redux/actionCreator/TabbarActionCreator' // import store from './redux/store' import {connect} from 'react-redux' function Detaill(props) { console.log(props.match.params.filmId) // 第一種方式路由獲取參數(shù) // console.log(props.location.query.filmId) // 第二種方式路由獲取參數(shù) // console.log(props.location.state.filmId) // 第三種方式路由獲取參數(shù) let {show, hide} = props useEffect(() => { console.log('創(chuàng)建') // store.dispatch(hide()) hide() return () => { console.log('銷毀') // store.dispatch(show()) show() } },[show,hide]) return ( <div>Detaill</div> ) } const mapDispatchToProps = { show, hide } // connect接收兩個(gè)參數(shù),第一個(gè)是屬性,第二個(gè)是回調(diào)函數(shù) export default connect(null, mapDispatchToProps)(Detaill);
可以看到這里都不需要我們?nèi)ispatch分發(fā)了,直接使用connect傳入到之前TabbarReducer寫好的類型以在子組件中以函數(shù)的形式去調(diào)用:
二、redux持久化
npm i redux-persist
修改store.js
代碼:
import { applyMiddleware, combineReducers, createStore, compose } from "redux"; import TabbarReducer from "./reducers/TabbarReducer"; import reduxThunk from "redux-thunk" import reduxPromise from "redux-promise" import { persistStore, persistReducer } from 'redux-persist' import storage from 'redux-persist/lib/storage' // defaults to localStorage for web const persistConfig = { key: 'TabbarReducer', storage, // blacklist: ['TabbarReducer'] // navigation will not be persisted 黑名單 whitelist: ['TabbarReducer'] // only navigation will be persisted 白名單 } const reducer = combineReducers({ TabbarReducer }) const persistedReducer = persistReducer(persistConfig, reducer) const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const store = createStore(persistedReducer, /* preloadedState, */ composeEnhancers( applyMiddleware(reduxThunk, reduxPromise) )); let persistor = persistStore(store) export {store, persistor};
然后在根組件中修改:
import FilmRouter from './FilmRouter/index' import {Provider} from 'react-redux' import {store, persistor} from './FilmRouter/views/redux/store'; import { PersistGate } from 'redux-persist/integration/react'; ReactDOM.render( <Provider store={store}> <PersistGate loading={null} persistor={persistor}> <FilmRouter /> </PersistGate> </Provider> , document.getElementById('root') );
效果:
原文鏈接:https://juejin.cn/post/7128766881019723784
相關(guān)推薦
- 2022-09-15 .Net站點(diǎn)設(shè)置多個(gè)路由對(duì)應(yīng)同一個(gè)Action_實(shí)用技巧
- 2023-11-18 Linux中ARM設(shè)備Linux 使用iperf3測(cè)量網(wǎng)絡(luò)帶寬
- 2022-09-14 React路由組件傳參的三種方式(params、search、state)_React
- 2022-04-14 遇到一個(gè)git的大坑 src refspec master does not match any e
- 2022-03-30 SQL基礎(chǔ)查詢和LINQ集成化查詢_MsSql
- 2022-06-02 一篇文章帶你了解Python中的裝飾器_python
- 2022-04-15 windows+vscode穿越跳板機(jī)調(diào)試遠(yuǎn)程代碼的圖文教程_python
- 2022-02-26 網(wǎng)頁(yè)小圖標(biāo)和文字混排時(shí)如何對(duì)齊基準(zhǔn)線
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支