<div className="catalog-mobile__counter" key={cart.find(item => item.product._id === product._id) ? cart.find(item => item.product._id === product._id).quantity : "" }>
<div className="catalog-mobile__minus"></div>
<input type="text" defaultValue={cart.find(item => item.product._id === product._id) ? cart.find(item => item.product._id === product._id).quantity : "" } />
<div className="catalog-mobile__plus"></div>
</div>
我该如何重构这段代码。这里有一个条件检查,因为不是所有的项目都会产生结果,并且有些未定义和错误,所以它是,但它看起来很糟糕。
如果我是你,我会用
JSX
. 在哪里key
给出了类似id
或独特的价值。有人猜测它quantity
可能会再次发生。可选的链接可用于缩短代码。如果您在使用 babel 时遇到错误,请安装以下组件[更多]: