简体   繁体   English

如何将数据从一个反应组件发送到另一个反应组件?

[英]How to send data from one react component to another react component?

Here I get some data from the 'Order' component and then I want to send this data to Navigate but all I get when using 'props.order_data' in 'Navigate' is undefined.在这里,我从“订单”组件中获取了一些数据,然后我想将此数据发送到 Navigate,但在“导航”中使用“props.order_data”时得到的所有数据都是未定义的。 How can I use this data in 'Navigate'?如何在“导航”中使用这些数据?

function App(){

  function getData(some){
    console.log(some);
    return(<Navigate order_data= {some}/>);
  }
  return(
    <div>
      <Navigate />
      <Service/>
      <Order onAdd={getData}/>
      <Review />
      <Foot />
  </div>
  );
}
export default App;

What you are trying to do won't work as the return-value of the getData function is passed to the Order -component's onAdd function.由于getData function 的返回值被传递给Order组件的onAdd function,因此您尝试执行的操作将不起作用。

I would suggest that you create a state for the order_data in your App-component and pass the state as a prop to the Navigate -element that you are returning.我建议您为 App 组件中的order_data创建一个 state 并将 state 作为道具传递给您要返回的Navigate元素。 You will need to update the state as soon as you retrieve the data (eg in getData function).检索数据后(例如在getData函数中),您将需要更新 state。

React will take care of updating the component as soon as the state changes.一旦 state 发生变化,React 将负责更新组件。

See React documentation for more details: https://reactjs.org/docs/lifting-state-up.html有关更多详细信息,请参阅 React 文档: https://reactjs.org/docs/lifting-state-up.html

You need to use useState hook to maintain a state.您需要使用useState挂钩来维护 state。 Since you have your Navigate and Order component in the same Parent App .由于您在同一个 Parent App中有NavigateOrder组件。

Do like this这样做

 import { useState } from "react";

 function App(){

  const [some, setSome] = useState(null); // Initial some value is null

  function getData(data){
    setSome(data); // This will update some state value
  }
  return(
    <div>
      <Navigate order_data={some}/> 
      <Service/>
      <Order onAdd={getData}/>
      <Review />
      <Foot />
  </div>
  );
}
export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过上下文将数据从一个组件发送到另一个组件? - how to send data From one component to another components with context in react? 当我按下按钮时如何将数据从一个组件发送到另一个组件。 React.js - how to send the data from one component to another component when i press a button. React.js 如何在 React 中不调用该组件的情况下将数据从一个组件发送到另一个组件? - How can I send data from one component to another without calling that component in React? 如何在React JS中将一个组件的值发送给另一个组件? - how to send value one component to another component in react js? 如何在 React 中使用 onclick 将数据从一个组件/div 发送到另一个 - How to send data from one component/div to another using onclick in React 将数据从一个组件传递到另一个组件 - Passing data from one component to another in react 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS 如何从一个 React 组件访问数据到另一个组件? - How to access data from one React component to another? 如何在反应中将数据从一个组件传输到另一个组件? - How to transfer data from one component to another in react? React:如何从一个组件获取API数据并将其传递给另一组件 - React: How to get API data from one component and pass it to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM