react redux及redux持久化示例详解

 更新时间:2022年08月08日 09:48:26   作者:你华还是你华  
这篇文章主要为大家介绍了react redux及redux持久化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、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')
);

效果:

以上就是react redux及redux持久化示例详解的详细内容,更多关于react redux持久化的资料请关注脚本之家其它相关文章!

相关文章

  • Zustand介绍与使用 React状态管理工具的解决方案

    Zustand介绍与使用 React状态管理工具的解决方案

    本文主要介绍了Zustand,一种基于React的状态管理库,Zustand以简洁易用、灵活性高及最小化原则等特点脱颖而出,旨在提供简单而强大的状态管理功能
    2024-10-10
  • 简单分析React中的EffectList

    简单分析React中的EffectList

    这篇文章主要简单分析了React中的EffectList,帮助大家更好的理解和学习使用React进行前端开发,感兴趣的朋友可以了解下
    2021-04-04
  • React合成事件原理解析

    React合成事件原理解析

    事件是在编程时系统内发生的动作或者发生的事情,而开发者可以某种方式对事件做出回应,而这里有几个先决条件,这篇文章主要介绍了React合成事件原理解析,需要的朋友可以参考下
    2022-07-07
  • React渲染机制超详细讲解

    React渲染机制超详细讲解

    React整个的渲染机制就是React会调用render()函数构建一棵Dom树,在state/props发生改变的时候,render()函数会被再次调用渲染出另外一棵树,重新渲染所有的节点,构造出新的虚拟Dom tree
    2022-11-11
  • 详解react-redux插件入门

    详解react-redux插件入门

    这篇文章主要介绍了详解react-redux插件入门,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • ReactNative环境搭建的教程

    ReactNative环境搭建的教程

    这篇文章主要介绍了ReactNative环境搭建的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • React配置代理服务器的5种方法及使用场景

    React配置代理服务器的5种方法及使用场景

    这篇文章主要介绍了React配置代理服务器的5种方法,无论使用哪种方法,都需要确保代理服务器的地址和端口正确,并且在配置完成后重新启动React开发服务器,使配置生效,需要的朋友可以参考下
    2023-08-08
  • React 组件传 children 的各种案例方案详解

    React 组件传 children 的各种案例方案详解

    自定义组件的时候往往需要传 children,由于写法比较多样,我就总结了一下,要自定义的组件其中包含一个 title 和一个 children,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2023-10-10
  • ReactQuery系列React Query 实践示例详解

    ReactQuery系列React Query 实践示例详解

    这篇文章主要为大家介绍了ReactQuery系列React Query 实践示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • React组件的使用详细讲解

    React组件的使用详细讲解

    React组件分为函数组件与class组件;函数组件是无状态组件,class称为类组件;函数组件只有props,没有自己的私有数据和生命周期函数;class组件有自己私有数据(this.state)和生命周期函数
    2022-11-11

最新评论