当前位置:首页 > React

react组件间如何传递参数

2026-03-11 10:12:34React

父子组件传参(Props)

父组件通过属性(props)向子组件传递数据。子组件通过props接收参数,React 的函数组件和类组件均可使用。

父组件传递参数:

<ChildComponent name="张三" age={25} />

子组件接收参数(函数组件):

function ChildComponent(props) {
  return <div>{props.name}, {props.age}</div>;
}

子组件接收参数(类组件):

class ChildComponent extends React.Component {
  render() {
    return <div>{this.props.name}, {this.props.age}</div>;
  }
}

子组件向父组件传参(回调函数)

父组件通过传递回调函数给子组件,子组件调用该函数并传递参数。

父组件定义回调:

function ParentComponent() {
  const handleData = (data) => {
    console.log("子组件传递的数据:", data);
  };
  return <ChildComponent onData={handleData} />;
}

子组件调用回调:

function ChildComponent({ onData }) {
  const sendData = () => {
    onData("来自子组件的数据");
  };
  return <button onClick={sendData}>传递数据</button>;
}

跨层级组件传参(Context)

通过 React 的 Context API 实现跨层级组件数据共享,避免逐层传递 props。

创建 Context:

const MyContext = React.createContext();

父组件提供数据:

function ParentComponent() {
  return (
    <MyContext.Provider value={{ user: "李四", id: 100 }}>
      <ChildComponent />
    </MyContext.Provider>
  );
}

子组件消费数据(函数组件):

function ChildComponent() {
  const context = React.useContext(MyContext);
  return <div>{context.user}, {context.id}</div>;
}

子组件消费数据(类组件):

class ChildComponent extends React.Component {
  static contextType = MyContext;
  render() {
    return <div>{this.context.user}, {this.context.id}</div>;
  }
}

全局状态管理(Redux)

通过 Redux 实现全局状态管理,适合复杂应用的多组件数据共享。

安装 Redux:

npm install redux react-redux

创建 Store 和 Reducer:

import { createStore } from 'redux';

const initialState = { count: 0 };
function reducer(state = initialState, action) {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    default:
      return state;
  }
}
const store = createStore(reducer);

Provider 包裹根组件:

import { Provider } from 'react-redux';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

组件中使用:

import { useSelector, useDispatch } from 'react-redux';

function Counter() {
  const count = useSelector(state => state.count);
  const dispatch = useDispatch();
  return (
    <div>
      <button onClick={() => dispatch({ type: 'INCREMENT' })}>+1</button>
      <span>{count}</span>
    </div>
  );
}

事件总线(Event Emitter)

通过第三方库(如 events)实现非父子组件间通信,适用于简单场景。

安装库:

npm install events

创建事件总线:

import { EventEmitter } from 'events';
const eventBus = new EventEmitter();

组件 A 发送事件:

eventBus.emit("eventName", { data: "参数" });

组件 B 监听事件:

react组件间如何传递参数

useEffect(() => {
  eventBus.on("eventName", (data) => {
    console.log("接收数据:", data);
  });
  return () => eventBus.off("eventName"); // 清除监听
}, []);

标签: 组件参数
分享给朋友:

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

uniapp如何将底部导航组件化

uniapp如何将底部导航组件化

在 uniapp 中将底部导航组件化 将底部导航组件化可以提高代码复用性和维护性。以下是具体实现方法: 创建自定义底部导航组件 新建一个组件文件,例如 tab-bar.vue,放置在 compon…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性来完成。以下是几种常见的实…

vue table组件实现

vue table组件实现

Vue 表格组件实现方案 基础表格实现 使用<table>标签结合v-for指令渲染数据是最简单的实现方式: <template> <table> &…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> &…