[英]React passing data into another component
I have read through the other posts.我已经阅读了其他帖子。
I need to pass the cartLength variable into the shopbanner.js
component, it needs to be imported from shop.js
我需要将cartLength变量传递到
shopbanner.js
部件,需要将其从进口shop.js
shop.js
export default function Shop() {
var cartLength = cart.length;
return (
...
);
}
shopbanner.js
import {cartLength} from '../pages/shop.js'
export default function ShopBanner() {
return (
<p>{cartLength}</p>
<img src="/basket.png"/>
);
}
Turns out I was way overcomplicating things thank you for your answers guys!原来我把事情复杂化了,谢谢你们的回答!
You can take cart length as an argument:您可以将购物车长度作为参数:
export default function ShopBanner(cartLength) {
return (
<p>{cartLength}</p>
<img src="/basket.png"/>
);
}
Then you can use it like a prop:然后你可以像道具一样使用它:
<ShopBanner cartLength={cart.length}>
You can use React's props argument.你可以使用 React 的 props 参数。
Add a props argument to the ShopBanner function.向 ShopBanner 函数添加一个 props 参数。 Then you can simply do:
然后你可以简单地做:
<ShopBanner cartLength=.... />
And then you can refrence the cartLength as props.cartLength in the ShopBanner's code.然后您可以在 ShopBanner 的代码中将 cartLength 引用为 props.cartLength。
您应该将其作为道具或通过某种状态管理器传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.