Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

es6: How to define functions inside a const?

I'm doing this react-native project and i am using this method to organize my project files but i don't know how to declare functions inside a 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>
    );
}
like image 388
bazi Avatar asked Nov 12 '16 08:11

bazi


1 Answers

What you called 'a const' is in fact an arrow function. In JS, you can add any nested functions as you want:

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>
    );
}
like image 94
Seb Bizeul Avatar answered Oct 19 '22 21:10

Seb Bizeul