Passing function from object to React Native ListView

designkai

I'm defining a function inside an object, and then passing it to a ListView.

However I'm having trouble getting the function to fire onPress. At the moment it's not doing anything when pressed.

export default class Settings extends Component {
    componentWillMount() {
        const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 })
        const listItems = [
            { title: 'Profile', icon: 'ios-contact', action: () => console.log('Profile') },
            { title: 'Log out', icon: 'ios-log-out', action: () => console.log('Log out') }
        ]
        this.setState({ dataSource: ds.cloneWithRows(listItems) })
    }

    render() {
        return (
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(item) => 
                    <TouchableOpacity onPress={() => item.action}>
                        <View>
                            <Ionicons name={item.icon} size={20} />
                            <Text>{item.title}</Text>
                        </View>
                    </TouchableOpacity>
                }
            />
        )
    }
};

I'm fairly new to React/JS, so I suspect it might be a syntax issue. Thanks!

sdkcy

You should remove fat arrow from TouchableOpacity onPress , like this

<TouchableOpacity onPress={item.action}>

This will work.

Edit: I created a project at snack. You can see that this approach fix your problem.

https://snack.expo.io/HJS68AjWN

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React Native: Passing specific object from array of object as props

From Dev

React Native passing Component/Object property to OnPress function

From Dev

Passing Object from a screen to another one | React-Native

From Dev

Passing state array from class into function with pure react native

From Dev

Passing string from native module to react native

From Dev

React-Native: Passing an if statement if the object is undefined

From Dev

React native passing variable inside a function

From Dev

Passing function arguments in react-native-boilerplate

From Dev

Passing function in React-Native audio player

From Dev

Passing a ref as function parameter React Native

From Dev

React-Native: Passing the value of a TextInput to a function

From Dev

passing className from the function in React

From Dev

React-Native ListView onPress - undefined is not an object

From Dev

React Native - ListView, CloneWithRow with object not array

From Dev

Looping nested array Object in ListView React native

From Dev

React Native - Passing data from a component to another

From Dev

React native passing response from request

From Dev

React native, passing variables from other files

From Dev

Passing values to functions in React Native from Array

From Dev

Passing array data from Swift to React Native

From Dev

Passing function pointer from native to java

From Dev

Passing event object in onClick function in React

From Dev

React native: remove item from ListView

From Dev

Populate ListView from web service in React Native

From Dev

react native dynamically change ListView from SQLite

From Dev

React Native "onPress is not a function" when passing function in Modal

From Dev

"'undefined' is not an object" from Native Module in React Native

From Dev

Passing object from virtual class as argument in a function

From Dev

Error passing values from function to a controller as an object

Related Related

  1. 1

    React Native: Passing specific object from array of object as props

  2. 2

    React Native passing Component/Object property to OnPress function

  3. 3

    Passing Object from a screen to another one | React-Native

  4. 4

    Passing state array from class into function with pure react native

  5. 5

    Passing string from native module to react native

  6. 6

    React-Native: Passing an if statement if the object is undefined

  7. 7

    React native passing variable inside a function

  8. 8

    Passing function arguments in react-native-boilerplate

  9. 9

    Passing function in React-Native audio player

  10. 10

    Passing a ref as function parameter React Native

  11. 11

    React-Native: Passing the value of a TextInput to a function

  12. 12

    passing className from the function in React

  13. 13

    React-Native ListView onPress - undefined is not an object

  14. 14

    React Native - ListView, CloneWithRow with object not array

  15. 15

    Looping nested array Object in ListView React native

  16. 16

    React Native - Passing data from a component to another

  17. 17

    React native passing response from request

  18. 18

    React native, passing variables from other files

  19. 19

    Passing values to functions in React Native from Array

  20. 20

    Passing array data from Swift to React Native

  21. 21

    Passing function pointer from native to java

  22. 22

    Passing event object in onClick function in React

  23. 23

    React native: remove item from ListView

  24. 24

    Populate ListView from web service in React Native

  25. 25

    react native dynamically change ListView from SQLite

  26. 26

    React Native "onPress is not a function" when passing function in Modal

  27. 27

    "'undefined' is not an object" from Native Module in React Native

  28. 28

    Passing object from virtual class as argument in a function

  29. 29

    Error passing values from function to a controller as an object

HotTag

Archive