当前位置:首页 > 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 动态组件实现方法 使用 component 标签与 is 属性 通过 Vue 内置的 <component> 标签和 is 属性实现动态组件切换。is 的值可以是已注册的组件名或导…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref="…

vue实现search组件

vue实现search组件

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

vue实现传递参数

vue实现传递参数

Vue 中传递参数的几种方法 通过路由传参(query 或 params) 使用 router-link 或编程式导航传递参数: // query 传参(显示在 URL 中) this.$route…

vue实现轮播组件

vue实现轮播组件

使用Swiper实现轮播组件 Swiper是一个流行的开源轮播库,支持Vue集成。安装Swiper和Vue相关依赖: npm install swiper vue-awesome-swiper 引入…

vue组件实现跳转

vue组件实现跳转

Vue 组件实现跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于…