react redux及redux持久化示例详解
作者:你华还是你华 时间:2023-07-17 07:18:53
一、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
标签:react,redux,持久化
0
投稿
猜你喜欢
利用Python制作一个MOOC公开课下载器
2022-03-13 19:46:17
Python3实现转换Image图片格式
2021-06-06 21:04:25
如何利用Python给自己的头像加一个小国旗(小月饼)
2023-09-15 20:47:31
ORACLE 10g 安装教程[图文]
2009-05-24 19:12:00
python使用wxPython打开并播放wav文件的方法
2023-08-23 10:30:58
mysql 实现添加时间自动添加更新时间自动更新操作
2024-01-14 15:58:59
Python编程中如何捕获警告ps不是捕获异常
2021-05-28 15:23:09
基于进程内通讯的python聊天室实现方法
2021-01-24 03:50:53
对Python信号处理模块signal详解
2021-08-20 04:06:11
ASP错误大全
2009-05-26 15:45:00
Python中str.format()方法的具体使用
2023-11-12 01:19:02
Anaconda环境克隆、迁移的详细步骤
2022-02-22 08:36:47
python interpolate插值实例
2022-05-06 08:20:37
显示日期格式的几个函数
2008-09-03 13:20:00
Python使用面向对象方式创建线程实现12306售票系统
2021-01-22 09:13:36
oracle 发送邮件 实现方法
2009-06-10 17:49:00
各种语言、服务器301跳转代码全集
2023-07-23 04:24:45
使用PHP开发Android应用程序技术介绍
2023-07-02 09:40:53
Python编程中的文件操作攻略
2022-12-08 10:49:54
再谈动态添加样式规则
2009-09-02 13:10:00