当前位置:首页 > React

react hook如何通信

2026-02-26 09:40:47React

React Hook 通信方法

React Hook 提供了多种方式实现组件间的通信,以下为常见方法:

useState + Props 父组件通过 useState 管理状态,子组件通过 props 接收数据和回调函数。

react hook如何通信

// 父组件
const Parent = () => {
  const [count, setCount] = useState(0);
  return <Child count={count} onIncrement={() => setCount(c => c + 1)} />;
};

// 子组件
const Child = ({ count, onIncrement }) => {
  return <button onClick={onIncrement}>Count: {count}</button>;
};

useContext 通过 Context API 实现跨层级组件通信,适合全局状态共享。

const CountContext = createContext();

// 父组件
const Parent = () => {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <Child />
    </CountContext.Provider>
  );
};

// 子组件
const Child = () => {
  const { count, setCount } = useContext(CountContext);
  return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
};

useReducer + Context 复杂状态管理场景下,结合 useReducer 和 Context 实现状态逻辑集中处理。

react hook如何通信

const CountContext = createContext();

// 父组件
const reducer = (state, action) => {
  switch (action.type) {
    case 'increment': return { count: state.count + 1 };
    default: throw new Error();
  }
};

const Parent = () => {
  const [state, dispatch] = useReducer(reducer, { count: 0 });
  return (
    <CountContext.Provider value={{ state, dispatch }}>
      <Child />
    </CountContext.Provider>
  );
};

// 子组件
const Child = () => {
  const { state, dispatch } = useContext(CountContext);
  return <button onClick={() => dispatch({ type: 'increment' })}>
    Count: {state.count}
  </button>;
};

自定义 Hook 封装共享逻辑为自定义 Hook,实现多组件复用相同状态逻辑。

const useCounter = () => {
  const [count, setCount] = useState(0);
  const increment = () => setCount(c => c + 1);
  return { count, increment };
};

// 组件A
const ComponentA = () => {
  const { count, increment } = useCounter();
  return <button onClick={increment}>A: {count}</button>;
};

// 组件B
const ComponentB = () => {
  const { count, increment } = useCounter();
  return <button onClick={increment}>B: {count}</button>;
};

第三方状态库 对于大型应用,可使用 Redux、Zustand 或 MobX 等库管理全局状态。

// 使用 Zustand 示例
import create from 'zustand';

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 }))
}));

// 组件
const Counter = () => {
  const { count, increment } = useStore();
  return <button onClick={increment}>Count: {count}</button>;
};

选择依据

  • 父子组件简单通信:useState + Props
  • 跨多层级组件:useContext
  • 复杂状态逻辑:useReducer + Context
  • 逻辑复用:自定义 Hook
  • 大型应用:第三方状态库

标签: 通信react
分享给朋友:

相关文章

如何优化react

如何优化react

优化 React 性能的方法 使用 React.memo 进行组件记忆 React.memo 是一个高阶组件,用于缓存函数组件的渲染结果,避免不必要的重新渲染。只有当组件的 props 发生变化时,才…

vue实现串口通信

vue实现串口通信

Vue 实现串口通信的方法 在 Vue 项目中实现串口通信,通常需要借助浏览器提供的 Web Serial API 或第三方库。以下是几种常见的实现方式: 使用 Web Serial API Web…

如何改造react

如何改造react

改造 React 项目的关键方法 分析当前项目结构 通过评估现有组件、状态管理和依赖项,明确需要改进的部分。使用工具如 webpack-bundle-analyzer 识别性能瓶颈。 升级 Reac…

如何启动react

如何启动react

安装Node.js 确保系统已安装Node.js(建议使用LTS版本),可通过官网下载并安装。安装完成后,在终端运行以下命令验证版本: node -v npm -v 创建React项目 使用官方工具…

如何构建react

如何构建react

构建 React 项目的步骤 安装 Node.js 和 npm 确保系统已安装 Node.js(包含 npm)。可通过命令行验证: node -v npm -v 使用 Create React Ap…

react 如何循环

react 如何循环

循环渲染列表 在React中,循环渲染列表通常使用map方法。map可以遍历数组并返回一个新的React元素数组。 const items = ['Apple', 'Banana', 'Orange…