当前位置:首页 > 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 转发,直接调用子组件方法或访问实例。

react组件间如何传递参数

// 子组件(函数组件需用 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>
    </>
  );
}

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

相关文章

vue实现折叠组件

vue实现折叠组件

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

vue抽屉组件实现

vue抽屉组件实现

Vue 抽屉组件实现 使用 Element UI 实现 Element UI 提供了现成的抽屉组件 el-drawer,可以快速实现抽屉效果。 安装 Element UI: npm install…

vue实现search组件

vue实现search组件

Vue 实现搜索组件 基础实现 创建一个基础的搜索组件需要包含输入框和搜索逻辑。以下是一个简单的实现示例: <template> <div class="search-cont…

vue 实现弹窗组件

vue 实现弹窗组件

Vue 弹窗组件实现 基础弹窗组件结构 创建一个名为Modal.vue的文件,定义弹窗的基础结构和样式: <template> <div class="modal-mask"…

vue实现路由组件

vue实现路由组件

Vue 实现路由组件的方法 在 Vue 中实现路由功能通常需要结合 Vue Router 库,以下是具体实现步骤: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Router…

vue实现组件刷新

vue实现组件刷新

强制重新渲染组件 使用 this.$forceUpdate() 方法可以强制 Vue 实例重新渲染,但不会更新子组件。适用于依赖非响应式数据的情况。 methods: { refreshComp…