React中Fragment的妙用,相当于Vue中的template

158 阅读1分钟

React中如果要渲染一个list列表, 然而你想要渲染的内容,大于一个根节点,有多个根节点,正常是用<></>包裹, 不过现在因为是List渲染,所以可以使用【Fragment】去包裹,这样就可以在标签上面加上key,使用上类似于Vue中的template

const renderList = list.map((item, index) => {  
return (  
<Fragment key={index}>  
<View>item</View>  
<View>item</View>
</Fragment>  
)  
})