当前位置:   article > 正文

hooks(useContext+useReducer)实现全局的状态管理_react hooks 全局状态管理

react hooks 全局状态管理

一个全局共享的状态不应该仅仅是共享,传递的作用,它也应该能被修改,做到一处修改,处处变化。

App.tsx

  1. import React,{useEffect,useReducer,useState} from 'react';
  2. // import logo from './logo.svg';
  3. import './App.css';
  4. import {
  5. // BrowserRouter as Router,
  6. Route,
  7. Link,
  8. // Router
  9. } from "react-router-dom";
  10. import {initState,context,countReducer} from './reducer/index'
  11. import Count from './components/Count'
  12. import ShowCount from './components/ShowCount'
  13. function App() {
  14. let store=useReducer(countReducer,initState)
  15. useEffect(()=>{
  16. console.log('store',store)
  17. })
  18. let linkArr=[
  19. {
  20. path:'/page1',
  21. label:'页面1'
  22. },
  23. {
  24. path:'/page2',
  25. label:'页面2'
  26. },
  27. {
  28. path:'/page3',
  29. label:'页面3'
  30. }
  31. ]
  32. let [currentIndex,setCurrentIndex]=useState(0)
  33. function changeCurrentIndex(index){
  34. setCurrentIndex(index)
  35. }
  36. return (
  37. <context.Provider value={store}>
  38. <div className="App">
  39. <ul className="list">
  40. {
  41. linkArr.map((item,index)=>{
  42. return <li className="list-item" onClick={(e)=>changeCurrentIndex(index,e)} key={index}><Link to={item.path} className={`${index===currentIndex?'active':''}`}>{item.label}</Link></li>
  43. })
  44. }
  45. </ul>
  46. <Route path='/page1' component={Page1}></Route>
  47. <Route path='/page2' component={Page2}></Route>
  48. <Route path='/page3' component={Page3}></Route>
  49. </div>
  50. </context.Provider>
  51. );
  52. }
  53. function Page1(){
  54. return (
  55. <div>
  56. <Count></Count>
  57. </div>
  58. )
  59. }
  60. function Page2(){
  61. return (
  62. <div>
  63. <ShowCount></ShowCount>
  64. </div>
  65. )
  66. }
  67. function Page3(){
  68. return (
  69. <div>
  70. this is page3
  71. </div>
  72. )
  73. }
  74. export default App;

app.js主要作用:在顶级的组件利用context.Provider,并向下面的组件共享唯一的store(包含state,和dispatch),下面的任一组件通过useContext得到state和dispatch,就能共享并且改变state值。如何得到我们需要的state和dispatch呢,就是通过useReducer得到的。

./reducer/index
 

  1. import React from 'react'
  2. let initState={
  3. count:0
  4. }
  5. function countReducer(state={},action){
  6. switch(action.type){
  7. case 'increment':
  8. return {count:state.count+1}
  9. case 'decrement':
  10. return {count:state.count-1 }
  11. default:
  12. return state
  13. }
  14. }
  15. let context =React.createContext()
  16. export {
  17. countReducer,
  18. initState,
  19. context
  20. }

这包含了reducer和iniState,还导出了context,每个用到共享状态的都需要用到此context,以保证所有的组件到用的是同一个context。

Count.js

  1. import React,{useContext} from 'react'
  2. import {context} from '../reducer/index'
  3. function Count(){
  4. let [state,dispatch]=useContext(context)
  5. return (
  6. <div>
  7. <h1>count:{state.count}</h1>
  8. <div>
  9. <button style={{width:'128px',height:'30px'}} onClick={()=>dispatch({type:'decrement'})}>-</button>
  10. <button style={{width:'128px',height:'30px'}} onClick={()=>dispatch({type:'increment'})}>+</button>
  11. </div>
  12. </div>
  13. )
  14. }
  15. export default Count

count.js主要作用:通过useContext得到state和dispatch就可以展示和修改state了。

ShowCount.js

  1. import React,{useContext} from 'react'
  2. import {context} from '../reducer/index'
  3. function ShowCount(){
  4. let [state]=useContext(context)
  5. return (
  6. <div>
  7. <h1>count:{state.count}</h1>
  8. </div>
  9. )
  10. }
  11. export default ShowCount

showCount.js作用和count.js差不多。只是这里只使用了state。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/550047
推荐阅读
相关标签
  

闽ICP备14008679号