網站首頁 編程語言 正文
一、react-redux
react-redux依賴于redux工作。 運行安裝命令:npm i react-redux
:
使用: 將Provider套在入口組件處,并且將自己的store傳進去:
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') );
然后在子組件導出的時候套一層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給傳入進去,那在子組件中就可以直接使用this.props來獲取這個值來決定是否渲染了:
可以看到效果還是跟之前一樣,只不過采用了react-redux,那么我們在Detaile組件中就不用自己去分發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) // 第一種方式路由獲取參數 // console.log(props.location.query.filmId) // 第二種方式路由獲取參數 // console.log(props.location.state.filmId) // 第三種方式路由獲取參數 let {show, hide} = props useEffect(() => { console.log('創建') // store.dispatch(hide()) hide() return () => { console.log('銷毀') // store.dispatch(show()) show() } },[show,hide]) return ( <div>Detaill</div> ) } const mapDispatchToProps = { show, hide } // connect接收兩個參數,第一個是屬性,第二個是回調函數 export default connect(null, mapDispatchToProps)(Detaill);
可以看到這里都不需要我們去dispatch分發了,直接使用connect傳入到之前TabbarReducer寫好的類型以在子組件中以函數的形式去調用:
二、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
相關推薦
- 2022-06-01 ASP.Net?Core中的日志與分布式鏈路追蹤_實用技巧
- 2022-06-14 Flutter網絡請求Dio庫的使用及封裝詳解_Android
- 2022-10-17 Python?pywin32實現word與Excel的處理_python
- 2022-07-02 C#并行編程之數據并行Tasks.Parallel類_C#教程
- 2022-05-12 Centos python3 與 python2 共存
- 2022-05-15 C++的數據共享與保護你了解嗎_C 語言
- 2022-04-09 在Mybatis中使用自定義緩存ehcache
- 2023-05-08 Python實現統計文章閱讀量的方法詳解_python
- 最近更新
-
- 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同步修改后的遠程分支