问题
In my nextJS app, I want to pass props of one component to another which is neither a parent nor child of first component.How can I do that?
There is a Order
component inside a div in Orders
page and OrderViewer
in another div.What I want is that when I click the 'View Order' button,orderno
of that order should pass to OrderViewer
component.
orders.js
<div>
<div><Order
orderno='abc'
title='abc'
status='abc'
entity='abc'
po='abc'
duedate='abc' /></div>
</div>
<div><OrderViewer /></div>
Order.js
<div>
<button>View Order</button>
<p><span>{this.props.orderno}</span>
<span>{this.props.title}</span></p>
<p>{this.props.entity} - {this.props.po}</p>
<p>Due {this.props.duedate}</p>
</div>
OrderViewer.js
<div>//should display the orderno of clicked `order`</div>
Once orderno
passed,Orderviewer
displays it.I saw so many this kind of 'passing props' questions here but none of them had this kind of situation.Any tip is highly appreciated..
回答1:
In your case you need to deal with callbacks. Callback is a concept in react.
Declare an event handler function in orders.js which sets the orderId to the state when button clicked in orders.js component and pass down that event handler function to Order component as props and pass down orderId as a prop to OrderViewer component
orders.js
constructor(props){
super(props);
this.state = {
orderId: null
}
}
handleViewOrder = orderId = {
this.setState({
orderId
});
}
render(){
return(<div>
<div><Order
orderno='abc'
title='abc'
status='abc'
entity='abc'
po='abc'
duedate='abc' handleViewOrder={this.handleViewOrder} /></div>
</div>
<div><OrderViewer orderId={this.state.orderId} /></div>)}
Now in order.js access the received handler function using props and assign it to button onClick by passing orderId
<div>
<button onClick={() => this.props.handleViewOrder(this.props.orderno)}>View Order</button>
<p><span>{this.props.orderno}</span>
<span>{this.props.title}</span></p>
<p>{this.props.entity} - {this.props.po}</p>
<p>Due {this.props.duedate}</p>
</div>
Now, you can access orderId using this.props.orderId in OrderViewer component and display it
OrderViewer.js
<div>{this.props.orderId}</div>
So having an event handler function in parent component and passing down as props to child component and assigning to child component button onClick and when clicked changing parent state is so called callbacks in react. If you understand what I am trying to explain in my answer you can get going easily
Edit:
In Order.js component
Change
<button onClick={() => this.handleViewOrder(this.props.orderno)}>View Order</button>
To
<button onClick={() => this.props.handleViewOrder(this.props.orderno)}>View Order</button>
Also you are calling Order component in loop but you are not setting unique key to Order component so
Change
{ Orders.map((order) => <Order
orderno={order.orderno}
title={order.title}
status={order.status}
entity={order.entity}
po={order.po}
duedate={order.duedate}
handleViewOrder={this.handleViewOrder}/> )}
To
{ Orders.map((order) => <Order
orderno={order.orderno}
title={order.title}
status={order.status}
entity={order.entity}
po={order.po}
key={order.orderno}
duedate={order.duedate}
handleViewOrder={this.handleViewOrder}/> )}
回答2:
So, reasoning is:
- you have some parent component/screen. Lets call it
OrdersPage
- within
OrdersPage
you have 2 components:Order
andOrdersViewer
- what you are interested in, within context of parent components (
OrdersPage
) is to know which order was clicked and show that in other child - obviously, you need
state
on yourOrdersPage
component - when you click some order, you will put it in state
- your other component will receive
state
asprops
and show clicked component
来源:https://stackoverflow.com/questions/54264486/pass-props-to-another-component-onclick-of-a-button