ES6:如何界定一个const里面的功能呢?

基础

我正在做这个本机反应项目,我正在使用这种方法来组织我的项目文件,但我不知道如何在const中声明函数。

import React from 'react';
import { View, ListView, Text } from 'react-native';

const Category = (props) => {

    const ds = ListView.DataSource({rowHasChanged : (r1, r2) => r1 !== r2});

    // how to declare function here?

    return (
        <View>
            <ListView
                dataSource={ds}
                renderRow={(rowData) => <Text>{rowData}</Text>}
                renderSeparator={// how to put function reference here?}
            />
        </View>
    );
}
勒布Bizeul

您所谓的“ const”实际上是一个箭头函数。在JS中,您可以根据需要添加任何嵌套函数:

const Category = (props) => {

    const ds = ListView.DataSource({rowHasChanged : (r1, r2) => r1 !== r2});

    // how to declare function here?

    // You can declare another arrow function if you want:
    const foo = () => console.log('arrow');

    // Or a standard function
    function bar() { console.log('standard'); }

    // Even a function returning a function :-)
    function baz() { return function() {...} }

    const renderCustomComponent = () => <div>____</div>

    return (
        <View>
            <ListView
                dataSource={ds}
                renderRow={(rowData) => <Text>{rowData}</Text>}
                renderSeparator={ renderCustomComponent } {/* Here goes your reference */}
            />
        </View>
    );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章