尝试使用映射方法生成列表项,它可以工作,但不能与参数函数一起使用

问题描述 投票:0回答:1

函数列表(){

const fruits = [
    { id: 1, name: 'orange', calories: 67 },
    { id: 2, name: 'banana', calories: 63 },
    { id: 3, name: 'pineapple', calories: 87 },
    { id: 4, name: 'coconut', calories: 69 },
    { id: 5, name: 'kiwi', calories: 56 },
    { id: 6, name: 'guava', calories: 36 }
]

 
const fruitlist = fruits.map(fruit => <li key={fruit.id}>{fruit.name} {fruit.calories}</li>)
 return (<ol>{fruitlist}</ol>)

}

导出默认列表

函数列表(){

const fruits = [
    { id: 1, name: 'orange', calories: 67 },
    { id: 2, name: 'banana', calories: 63 },
    { id: 3, name: 'pineapple', calories: 87 },
    { id: 4, name: 'coconut', calories: 69 },
    { id: 5, name: 'kiwi', calories: 56 },
    { id: 6, name: 'guava', calories: 36 }
]

//这不起作用

let fruitslist = fruits.map(function (frt) {
    <li  key={fruit.id}>{frt.name}{frt.calories}</li>
})
return (<ol>{fruitlist}</ol>)

}

导出默认列表

javascript reactjs
1个回答
0
投票

箭头函数有 2 种主要的返回值方式,通过显式

return
关键字或隐式。


// No body braces and word "return" — the return is implied.
(a) => a + 100;

// With a body braces and the word "return" - the return is explicit.
(a) => {
  return a + 100;
}

您可以在文档中阅读更多相关信息。

© www.soinside.com 2019 - 2024. All rights reserved.