zoukankan      html  css  js  c++  java
  • useReducer 和 useContext 类似readux

    import React, { useReducer } from 'react'
    import './App.css'
    import ComponentA from './components/ComponentA'
    import ComponentB from './components/ComponentB'
    import ComponentC from './components/ComponentC'
    const initialState = 0
    const reducer = (state, action) => {
        switch (action) {
            case 'increment':
                return state + 1
            case 'decrement':
                return state - 1
            case 'reset':
                return initialState
            default:
                return state
        }
    }
    
    export const CountContext = React.createContext()
    
    function App() {
        const [count, dispatch] = useReducer(reducer, initialState)
        return (
            <CountContext.Provider
                value={{ countState: count, countDispatch: dispatch }}
            >
                <div className="App">
                    {count}
                    <ComponentA />
                    <ComponentB />
                                    <ComponentC />
                </div>
            </CountContext.Provider>
        )
    }
    
    export default App    
    
    
    import React, {useContext} from 'react'
    import { CountContext } from '../App';
    //组件A
    function ComponentA() {
      const countContext = useContext(CountContext)
      return (
        <div>
          Component A {countContext.countState}
          <button onClick={() => countContext.countDispatch('increment')}>Increment</button>
                <button onClick={() => countContext.countDispatch('decrement')}>Decrement</button>
                <button onClick={() => countContext.countDispatch('reset')}>Reset</button>
        </div>
      )
    }
    
    export default ComponentA
    //组件B
    import React, {useContext} from 'react'
    import { CountContext } from '../App';
    
    function ComponentB() {
      const countContext = useContext(CountContext)
      return (
        <div>
          Component F {countContext.countState}
          <button onClick={() => countContext.countDispatch('increment')}>Increment</button>
                <button onClick={() => countContext.countDispatch('decrement')}>Decrement</button>
                <button onClick={() => countContext.countDispatch('reset')}>Reset</button>
        </div>
      )
    }
    
    export default ComponentB
    
    import React, {useContext} from 'react'
    import { CountContext } from '../App';
    //组件C  其他依此类推
    function ComponentC() {
      const countContext = useContext(CountContext)
      return (
        <div>
          Component F {countContext.countState}
          <button onClick={() => countContext.countDispatch('increment')}>Increment</button>
                <button onClick={() => countContext.countDispatch('decrement')}>Decrement</button>
                <button onClick={() => countContext.countDispatch('reset')}>Reset</button>
        </div>
      )
    }
    
    export default ComponentC
         
  • 相关阅读:
    linux 程序后台运行
    小型网站架构技术点(简要)
    rsync安装与配置使用 数据同步方案(centos6.5)
    nfs的原理 安装配置方法 centos6.5
    centos 6.5 升级到 python2.7
    ntpdate 设置时区(注意本地时区要设置正确)
    关于umask的计算方式(简单任性)
    No space left on device(总结)
    lsof 查看打开了一个文件的有哪些进程 统计那个进程打开的文件最多
    作用域是什么?
  • 原文地址:https://www.cnblogs.com/chenzxl/p/13224943.html
Copyright © 2011-2022 走看看