当前位置:首页 > React

react组件间如何传递参数

2026-01-25 01:17:17React

通过 props 传递参数

父组件通过属性(props)向子组件传递数据。子组件通过 this.props(类组件)或直接解构 props(函数组件)接收参数。

// 父组件
<ChildComponent name="John" age={25} />

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

// 子组件(函数组件)
function ChildComponent({ name, age }) {
  return <div>{name}, {age}</div>;
}

使用 Context API 跨层级传递

通过 React.createContext 创建上下文,父组件用 Provider 提供数据,子组件用 ConsumeruseContext 钩子获取数据。

// 创建上下文
const MyContext = React.createContext();

// 父组件提供数据
<MyContext.Provider value={{ user: "Alice", id: 1 }}>
  <ChildComponent />
</MyContext.Provider>

// 子组件消费数据(类组件)
class ChildComponent extends React.Component {
  render() {
    return (
      <MyContext.Consumer>
        {value => <div>{value.user}</div>}
      </MyContext.Consumer>
    );
  }
}

// 子组件消费数据(函数组件)
function ChildComponent() {
  const value = useContext(MyContext);
  return <div>{value.user}</div>;
}

通过回调函数传递数据

子组件通过父组件传递的回调函数,将数据回传给父组件。

// 父组件
function ParentComponent() {
  const handleData = (data) => {
    console.log(data);
  };
  return <ChildComponent onData={handleData} />;
}

// 子组件
function ChildComponent({ onData }) {
  const sendData = () => {
    onData("Hello from child");
  };
  return <button onClick={sendData}>Send</button>;
}

使用状态管理库(如 Redux)

通过全局状态管理实现任意组件间的数据共享。

// 定义 action 和 reducer
const setUser = (user) => ({ type: 'SET_USER', payload: user });

function userReducer(state = null, action) {
  switch (action.type) {
    case 'SET_USER': return action.payload;
    default: return state;
  }
}

// 组件中调用
import { useDispatch, useSelector } from 'react-redux';

function ComponentA() {
  const dispatch = useDispatch();
  dispatch(setUser("Bob"));
}

function ComponentB() {
  const user = useSelector(state => state.user);
  return <div>{user}</div>;
}

通过 ref 传递数据或方法

使用 React.forwardRef 实现 ref 转发,直接调用子组件方法或访问实例。

// 子组件(函数组件需用 forwardRef)
const ChildComponent = React.forwardRef((props, ref) => {
  useImperativeHandle(ref, () => ({
    showAlert: () => alert("Child method called")
  }));
  return <div>Child</div>;
});

// 父组件使用
function ParentComponent() {
  const childRef = useRef();
  const callChildMethod = () => {
    childRef.current.showAlert();
  };
  return (
    <>
      <ChildComponent ref={childRef} />
      <button onClick={callChildMethod}>Call</button>
    </>
  );
}

react组件间如何传递参数

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

相关文章

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template…

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发自…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

react如何传参数

react如何传参数

在 React 中传递参数的方法 React 提供了多种传递参数的方式,具体取决于组件的类型和场景。以下是常见的参数传递方法: 父子组件传递参数(Props) 父组件通过属性(props)向子组件传…

vue 实现toast组件

vue 实现toast组件

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