• HOC的运用


    1. 防抖高阶组件 (Debounce HOC)

    1. import React, { useState, useEffect } from 'react';
    2. const debounce = (func, delay) => {
    3. let timer;
    4. return function(...args) {
    5. clearTimeout(timer);
    6. timer = setTimeout(() => func.apply(this, args), delay);
    7. };
    8. };
    9. const withDebounce = (Component, delay) => {
    10. return function(props) {
    11. const [inputValue, setInputValue] = useState('');
    12. const handleChange = (e) => {
    13. setInputValue(e.target.value);
    14. };
    15. useEffect(() => {
    16. const handleDebounce = debounce(props.onChange, delay);
    17. handleDebounce(inputValue);
    18. }, [inputValue]);
    19. return <Component {...props} inputValue={inputValue} onChange={handleChange} />;
    20. };
    21. };
    22. export default withDebounce;

    使用示例:

    1. import React from 'react';
    2. import withDebounce from './withDebounce';
    3. const SearchInput = (props) => {
    4. return <input type="text" value={props.inputValue} onChange={props.onChange} />;
    5. };
    6. const DebouncedSearchInput = withDebounce(SearchInput, 300);
    7. export default DebouncedSearchInput;

    2. 权限控制高阶组件 (Authorization HOC)

    1. import React from 'react';
    2. const withAuthorization = (Component, requiredRole) => {
    3. return function(props) {
    4. const userRole = getLoggedInUserRole();
    5. if (userRole !== requiredRole) {
    6. return <div>You do not have the required permissions to view this page.</div>;
    7. }
    8. return <Component {...props} />;
    9. };
    10. };
    11. export default withAuthorization;

    使用示例:

    1. import React from 'react';
    2. import withAuthorization from './withAuthorization';
    3. const AdminPanel = () => {
    4. return <div>This is the admin panel.</div>;
    5. };
    6. const AuthorizedAdminPanel = withAuthorization(AdminPanel, 'admin');
    7. export default AuthorizedAdminPanel;

    3. 异步加载高阶组件 (Async Loading HOC)

    1. import React, { useState, useEffect } from 'react';
    2. const withAsyncLoading = (Component) => {
    3. return function(props) {
    4. const [data, setData] = useState(null);
    5. const [isLoading, setIsLoading] = useState(true);
    6. useEffect(() => {
    7. fetchData().then((data) => {
    8. setData(data);
    9. setIsLoading(false);
    10. });
    11. }, []);
    12. if (isLoading) {
    13. return <div>Loading...</div>;
    14. }
    15. return <Component {...props} data={data} />;
    16. };
    17. };
    18. export default withAsyncLoading;

    使用示例:

    1. import React from 'react';
    2. import withAsyncLoading from './withAsyncLoading';
    3. const DataDisplay = (props) => {
    4. return <div>{props.data}</div>;
    5. };
    6. const AsyncDataDisplay = withAsyncLoading(DataDisplay);
    7. export default AsyncDataDisplay;

    4. 状态管理高阶组件 (State Management HOC)

    1. import React, { useState } from 'react';
    2. const withState = (Component) => {
    3. return function(props) {
    4. const [count, setCount] = useState(0);
    5. const incrementCount = () => {
    6. setCount(count + 1);
    7. };
    8. const decrementCount = () => {
    9. setCount(count - 1);
    10. };
    11. return (
    12. <Component
    13. {...props}
    14. count={count}
    15. incrementCount={incrementCount}
    16. decrementCount={decrementCount}
    17. />
    18. );
    19. }
    20. }
    21. export default withState;

    使用示例:

    1. import React from 'react';
    2. import withState from './withState';
    3. const Counter = (props) => {
    4. return (
    5. <div>
    6. <button onClick={props.incrementCount}>+</button>
    7. <span>{props.count}</span>
    8. <button onClick={props.decrementCount}>-</button>
    9. </div>
    10. );
    11. };
    12. const CounterWithState = withState(Counter);
    13. export default CounterWithState;

    5. 日志记录高阶组件 (Logging HOC)

    1. import React, { useEffect } from 'react';
    2. const withLogging = (Component) => {
    3. return function(props) {
    4. useEffect(() => {
    5. console.log('Component mounted');
    6. return () => {
    7. console.log('Component unmounted');
    8. };
    9. }, []);
    10. return <Component {...props} />;
    11. };
    12. };
    13. export default withLogging;

    使用示例:

    1. import React from 'react';
    2. import withLogging from './withLogging';
    3. const MyComponent = () => {
    4. return <div>Hello World!</div>;
    5. };
    6. const LoggedComponent = withLogging(MyComponent);
    7. export default LoggedComponent;
  • 相关阅读:
    图的应用之最小生成树
    【低码】asp.net core 实体类可生产 CRUD 后台管理界面
    对话杨传辉:国产数据库新战绩背后,OceanBase坚持自研的初心与决心
    第3章:搜索与图论【AcWing】
    IDEA代码重构技巧--抽取类和接口
    在 MySQL 和 PostgreSQL 中存储三元数据
    【OpenCV】车辆识别 C++ OpenCV 原理介绍 + 案例实现
    AK F.*ing leetcode 流浪计划之半平面求交
    数据结构(Java):力扣&牛客 二叉树面试OJ题
    LeetCode高频题84. 柱状图中最大的矩形,单调栈求位置i左边右边距离i最近且比i位置小的位置,然后结算面积
  • 原文地址:https://blog.csdn.net/m0_47999208/article/details/133267017