22 react hooks useReducer, createContext

import React, { useReducer, createContext } from 'react'
let NumContext = createContext()
//useReducer 数据管理器 (类似于redux中的reducer)
export default function Comp17() {
    //useReducer(reducer.js导出的函数,reducer.js中的defaultState)
    //这个方法返回 state对象用于渲染数据 和 dispatch函数用于修改数据
    let [state, dispatch] = useReducer(numReducer, { num: 20 })
    const addclick = () => {
        dispatch({
            type: "toAdd",
            value: 10
        })
    }
    return (
        <NumContext.Provider value={{ state, addclick }}>
            <Title />
            <Btn />
        </NumContext.Provider>
    )
}

function Title() {
    return (
        <NumContext.Consumer>
            {
                ({ state }) => {
                    return (
                        <div>{state.num}</div>
                    )
                }
            }

        </NumContext.Consumer>
    )
}

function Btn() {
    return (
        <NumContext.Consumer>
            {
                ({ addclick }) => {
                    return (
                        <button onClick={addclick}>add</button>
                    )
                }
            }

        </NumContext.Consumer>
    )
}

function numReducer(state, action) {
    let newState = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case 'toAdd':
            newState.num = newState.num + action.value
            break;
        default:
            break;
    }
    return newState;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Hooks 是 React 16.8 中新增的特性,它允许函数组件中使用 state 和其他 React 特性,从而使函数组件具有类组件的能力。 使用 React Hooks 需要先引入 ReactuseState、useEffect 等钩子函数,然后在函数组件中使用它们。 useState useState 是最常用的 Hook 之一,它可以让我们在函数组件中使用 state。useState 接收一个初始值作为参数,并返回一个数组,数组的第一个元素是当前 state 的值,第二个元素是更新 state 的函数。 例如,下面的代码在函数组件中使用了 useState 来保存一个计数器: ``` import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } ``` useEffect useEffect 是另外一个常用的 Hook,它可以在函数组件中使用副作用。副作用包括数据获取、订阅或手动修改 DOM 等操作。useEffect 接收一个函数作为参数,该函数会在组件渲染完成后执行。 例如,下面的代码使用 useEffect 来更新页面标题: ``` import React, { useState, useEffect } from 'react'; function PageTitle() { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; }); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } ``` useContext useContext 可以让我们在函数组件中使用 ContextContext 是一种在组件树中传递数据的方法,它可以避免通过 props 层层传递数据。 例如,下面的代码使用 useContext 来获取全局的主题: ``` import React, { useContext } from 'react'; const ThemeContext = React.createContext('light'); function ThemeButton() { const theme = useContext(ThemeContext); return ( <button style={{ background: theme.background, color: theme.foreground }}> I am styled by theme context! </button> ); } ``` 使用 useContext 前需要先创建一个 Context,可以使用 React.createContext 方法来创建。 除了上述三个 Hook,还有 useReducer、useCallback、useMemo、useRef 等 Hook 可以使用。使用这些 Hook 可以让函数组件更加强大和灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值