我有一段代码,在react中的inline if else中有很长的条件,我可以通过在内联语句中放置一个函数来使其简短吗?
我尝试这样做,但它给了我一个错误“折扣未定义”
function getDiscount(props) {
const discount = props.discount;
discount.map((discounts, i) => {
if (condition) {
return;
} else {
return;
}
})
}
{props.discount ? <getDiscount discount={discount} /> : <b>No available discount</b>}
最佳答案
将折扣传递给 getDiscount
组件时,您需要从 props 中传递它,例如
{props.discount ? <getDiscount discount={props.discount} /> : <b>No available discount</b>}
关于javascript - 在 React 中 if else 可以内联运行函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50042499/