当前位置:首页 > React

react实现请求组件

2026-01-26 17:17:17React

请求组件的实现方式

在React中实现请求组件通常涉及封装HTTP请求逻辑,提供统一的加载状态、错误处理和结果展示。以下是几种常见实现方法:

封装自定义Hook

使用React Hook封装请求逻辑是最现代的方式:

import { useState, useEffect } from 'react';

function useFetch(url) {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await fetch(url);
        if (!response.ok) throw new Error(response.statusText);
        const json = await response.json();
        setData(json);
      } catch (err) {
        setError(err.message);
      } finally {
        setLoading(false);
      }
    };

    fetchData();
  }, [url]);

  return { data, loading, error };
}

高阶组件方式

对于类组件或需要复用逻辑的场景可以使用高阶组件:

function withFetch(WrappedComponent, url) {
  return class extends React.Component {
    state = {
      data: null,
      loading: true,
      error: null
    };

    componentDidMount() {
      fetch(url)
        .then(res => res.json())
        .then(data => this.setState({ data, loading: false }))
        .catch(error => this.setState({ error, loading: false }));
    }

    render() {
      return <WrappedComponent {...this.props} {...this.state} />;
    }
  };
}

使用Axios的请求组件

结合Axios实现更强大的请求组件:

import axios from 'axios';

function RequestComponent({ url, children }) {
  const [state, setState] = useState({
    data: null,
    loading: true,
    error: null
  });

  useEffect(() => {
    axios.get(url)
      .then(response => {
        setState({
          data: response.data,
          loading: false,
          error: null
        });
      })
      .catch(error => {
        setState({
          data: null,
          loading: false,
          error: error.message
        });
      });
  }, [url]);

  return children(state);
}

请求状态管理

对于复杂应用,建议将请求状态纳入全局状态管理:

// 使用Context API
const ApiContext = createContext();

function ApiProvider({ children }) {
  const [apiState, dispatch] = useReducer(apiReducer, initialState);

  const fetchData = async (url) => {
    dispatch({ type: 'FETCH_START' });
    try {
      const response = await fetch(url);
      const data = await response.json();
      dispatch({ type: 'FETCH_SUCCESS', payload: data });
    } catch (error) {
      dispatch({ type: 'FETCH_ERROR', payload: error.message });
    }
  };

  return (
    <ApiContext.Provider value={{ ...apiState, fetchData }}>
      {children}
    </ApiContext.Provider>
  );
}

错误边界处理

为请求组件添加错误边界:

class ErrorBoundary extends React.Component {
  state = { hasError: false };

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    console.error('Request Error:', error, info);
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback;
    }
    return this.props.children;
  }
}

// 使用方式
<ErrorBoundary fallback={<div>请求出错</div>}>
  <RequestComponent url="/api/data">
    {({ data }) => <div>{data}</div>}
  </RequestComponent>
</ErrorBoundary>

请求取消功能

实现可取消的请求:

function useCancelableFetch(url) {
  const [state, setState] = useState({
    data: null,
    loading: true,
    error: null
  });

  useEffect(() => {
    const controller = new AbortController();
    const signal = controller.signal;

    fetch(url, { signal })
      .then(res => res.json())
      .then(data => setState({ data, loading: false, error: null }))
      .catch(err => {
        if (err.name !== 'AbortError') {
          setState({ data: null, loading: false, error: err.message });
        }
      });

    return () => controller.abort();
  }, [url]);

  return state;
}

以上实现方式可根据具体需求选择或组合使用,现代React应用推荐优先使用自定义Hook方案。

react实现请求组件

标签: 组件react
分享给朋友:

相关文章

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…

react如何保养

react如何保养

保持组件简洁 将大型组件拆分为更小、更专注的组件,每个组件只负责单一功能。避免在单个组件中处理过多逻辑或状态,这有助于提高可维护性和可测试性。 合理使用状态管理 根据应用复杂度选择状态管理方案。简…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div> &…

vue穿梭框组件实现

vue穿梭框组件实现

实现 Vue 穿梭框组件的基本思路 穿梭框(Transfer)组件通常用于在两个列表之间移动数据项。核心功能包括左侧列表、右侧列表、移动按钮(左移、右移、全选等)以及数据项的渲染与交互。 基础结构设…

如何生成react代码

如何生成react代码

使用 Create React App 生成项目 安装 Node.js 后,通过命令行工具运行以下命令创建新项目: npx create-react-app my-app cd my-app npm…