简体   繁体   English

在反应中将变量从一个组件发送到另一个组件

[英]Send variable from one component to another in react

I have created a routing where the component ProductId will be rendered (a href="/id")when there is a click on div with class name="card"我创建了一个路由,当单击带有 class name="card" 的 div 时,将在其中呈现组件 ProductId (a href="/id")

 <a href="/id"> <div className="card"> <div className="imgBx"> <h1>Product</h1> <img src={image} alt={image} className="myProductImage" /> </div> <div className="content"> <div className="productName"> <h3>{name}</h3> </div> <div className="price_rating"> <div className="price">{price} $</div> <div className="ratings"> {ratings} <i className="fa fa-star"></i> </div> </div> </div> </div> </a>

The Route is defined as following:路由定义如下:

 ReactDOM.render( <BrowserRouter> <Switch> <Route path="/id" component={ProductId}/> <Route path="/" exact component={Products}/> </Switch> </BrowserRouter>,document.getElementById("root") );

Now when the component ProductId is rendered I want to pass props (_id), (this props is present in the component where the onClick event is taking place) with onClick but since I am not calling the directly I cannot pass the props like.现在,当呈现组件 ProductId 时,我想通过 onClick 传递道具 (_id),(此道具存在于发生 onClick 事件的组件中),但由于我没有直接调用,所以我无法像这样传递道具。

Is there any other way where I can pass the props to the ProductId component when there is an onClick event on div with class name="card"当 class name="card" 上的 div 上有 onClick 事件时,有没有其他方法可以将道具传递给 ProductId 组件

If i understand correctly, you should use a dynamic route that contain the product id如果我理解正确,您应该使用包含产品id的动态路由

For example:例如:

<Router>
  <Route path="/product/:id" component={ProductId} />
</Router>

Then your ProductId component will receive the id from the router params in props.match.params.id然后您的ProductId组件将从props.match.params.id中的路由器参数接收id

see https://reactrouter.com/web/api/Route/component参见https://reactrouter.com/web/api/Route/component

暂无
暂无

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

相关问题 如何将数据从一个反应组件发送到另一个反应组件? - How to send data from one react component to another react component? 如何将变量的值从一个组件发送到另一个组件? (反应) - How can I send the value of a variable from one component to another? (React) 在 REACT 中将过滤器变量从一个组件导入到另一个组件 - Importing a filter variable from one Component to Another Component in REACT react-native:将完整的STATE从一个组件发送到另一个组件? - react-native: send complete STATE from one component to another? 如何通过上下文将数据从一个组件发送到另一个组件? - how to send data From one component to another components with context in react? 从一个 React 组件向另一个组件发送 javascript object - Send a javascript object from one React component to another 当我按下按钮时如何将数据从一个组件发送到另一个组件。 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? 如何将价值从一个组件发送到另一个组件 - How to send value from one component, to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM