HOC的运用

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


import React, { useState, useEffect } from 'react';
const debounce = (func, delay) => {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => func.apply(this, args), delay);
  };
};
const withDebounce = (Component, delay) => {
  return function(props) {
    const [inputValue, setInputValue] = useState('');
    const handleChange = (e) => {
      setInputValue(e.target.value);
    };
    
    useEffect(() => {
      const handleDebounce = debounce(props.onChange, delay);
      handleDebounce(inputValue);
    }, [inputValue]);
    return ;
  };
};
export default withDebounce;

使用示例:


import React from 'react';
import withDebounce from './withDebounce';
const SearchInput = (props) => {
  return ;
};
const DebouncedSearchInput = withDebounce(SearchInput, 300);
export default DebouncedSearchInput;

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


import React from 'react';
const withAuthorization = (Component, requiredRole) => {
  return function(props) {
    const userRole = getLoggedInUserRole();
    if (userRole !== requiredRole) {
      return 
You do not have the required permissions to view this page.
; } return ; }; }; export default withAuthorization;

使用示例:


import React from 'react';
import withAuthorization from './withAuthorization';
const AdminPanel = () => {
  return 
This is the admin panel.
; }; const AuthorizedAdminPanel = withAuthorization(AdminPanel, 'admin'); export default AuthorizedAdminPanel;

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


import React, { useState, useEffect } from 'react';
const withAsyncLoading = (Component) => {
  return function(props) {
    const [data, setData] = useState(null);
    const [isLoading, setIsLoading] = useState(true);
    useEffect(() => {
      fetchData().then((data) => {
        setData(data);
        setIsLoading(false);
      });
    }, []);
    if (isLoading) {
      return 
Loading...
; } return ; }; }; export default withAsyncLoading;

使用示例:


import React from 'react';
import withAsyncLoading from './withAsyncLoading';
const DataDisplay = (props) => {
  return 
{props.data}
; }; const AsyncDataDisplay = withAsyncLoading(DataDisplay); export default AsyncDataDisplay;

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


import React, { useState } from 'react';
const withState = (Component) => {
  return function(props) {
    const [count, setCount] = useState(0);
    const incrementCount = () => {
      setCount(count + 1);
    };
    const decrementCount = () => {
      setCount(count - 1);
    };
    return (
      
    );
  }
}
export default withState;

使用示例:


import React from 'react';
import withState from './withState';
const Counter = (props) => {
  return (
    
{props.count}
); }; const CounterWithState = withState(Counter); export default CounterWithState;

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


import React, { useEffect } from 'react';
const withLogging = (Component) => {
  return function(props) {
    useEffect(() => {
      console.log('Component mounted');
      return () => {
        console.log('Component unmounted');
      };
    }, []);
    return ;
  };
};
export default withLogging;

使用示例:


import React from 'react';
import withLogging from './withLogging';
const MyComponent = () => {
  return 
Hello World!
; }; const LoggedComponent = withLogging(MyComponent); export default LoggedComponent;

你可能感兴趣的:(#,react,javascript,react.js,前端)