SectionList中的renderItem方法和sectionHeader方法中的参数一个加了大括号一个没加大括号

来源:3-7 高性能列表组件SectionList详解-2

慕无忌King

2018-09-14

在SectionListDemo中的两个方法一个是_renderSectionHeader一个是_renderItem。
这两个方法,都需要传入一个参数,根据参数展示内容。
其中的renderItem方法

 _renderItem=(data)=>{
       
        return (
            <View style={styles.item}>
               省略。。。
            </View>
        )
    }

参数是直接给的data,而在_renderSectionHeader方法中

_renderSectionHeader=({section})=>{
        return (
            <View>
                省略...
            </View>
        )
    }

参数section是以{section}的形式给的。
这两个为什么一个给大括号一个不给大括号呢。

写回答

1回答

CrazyCodeBoy

2018-09-15

是这样的:

SectionList的renderItem与renderSectionHeader属性都是function类型,这个function的参数是一个object:

renderItem的object参数包含:'item' ,'index' ,'section' 'separators' 这几个属性;

renderSectionHeader的object参数包含:'section'属性。

_renderItem=(data)=>的data就代表这个object参数;
_renderSectionHeader=({section})=>的section代表这个object的中的section属性

renderSectionHeader=({section})=>用到了ES6的特性解构赋值,将object中的section属性取出。

renderSectionHeader=({section})=>{}
可替换为:
renderSectionHeader=(data)=>{cosnt {section}=data;}

可参考:

https://facebook.github.io/react-native/docs/sectionlist#renderitem

http://www.devio.org/2018/09/09/ES6-ES7-ES8-Feature




1
1
慕无忌King
非常感谢!
2018-09-17
共1条回复

React Native技术精讲与高质量上线App开发

一个真实的上线项目,一次完整的开发过程,全面掌握React Native技术

1577 学习 · 727 问题

查看课程