1

I have tried all the possible solutions that I know of. I have tried several other solutions to similar problems but I do not understand how this is supposed to work. I am trying to call a function from inside a ListView renderRow method. I can now call the function but I cannot pass in the selected rowData that I need. This is what I am getting:

screenshot

I need the selected rowData. here is code

     constructor(props) {
        super(props);
        this.onSubmitTrip = this.onSubmitTrip.bind(this);
        this.onSelectTrip = this.onSelectTrip.bind(this);
        this.renderRow = this.renderRow.bind(this);

        this.state = {
            dataSource: ds,
            showSubmitTripBtn: false
        }
    }
    onSelectTrip(rowData) {
        selectedTrip = rowData;
    }

  renderRow(rowData) {
        return (
            <TouchableHighlight onPress={this.onSelectTrip}  underlayColor='#dddddd'>
                <View style={styles.row}>
                    <View style={{ paddingLeft: 5 }}>
                        <View>
                            <Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
                        </View>
                    </View>
                </View>
            </TouchableHighlight>
        )
    }
1
  • "I can now call the function but I cannot pass in the selected rowData that I need." I don't see where you are calling the method. Passing the data is as simple as this.renderRow(theData). Commented May 10, 2016 at 18:16

2 Answers 2

1

Okay from what I can see you aren't actually passing onSelectTrip your rowData when your TouchableHighlight is pressed.

// Change this
<TouchableHighlight
  onPress={this.onSelectTrip}
  underlayColor='#dddddd'
>

// To this
<TouchableHighlight
  onPress={() => this.onSelectTrip(rowData)}
  underlayColor='#dddddd'
>

Also while looking at your screenshot I noticed another potential issue further down in the onSelectTrip method with your lodash _.each call. You need to pass _.each a fat arrow as the callback if you want to use this.setState() inside of it.

// Change this
_.each(rowData.routeLocations, function(item) {

// To this
_.each(rowData.routeLocations, (item) => {
Sign up to request clarification or add additional context in comments.

Comments

1

How about using the ES6 lambda's instead of explicitly binding all the methods in the class constructor to provide the lexical binding. I think this should solve your issue:

constructor(props) {
    super(props);
    this.onSubmitTrip = this.onSubmitTrip.bind(this);
    this.renderRow = this.renderRow.bind(this);

    this.state = {
        dataSource: ds,
        showSubmitTripBtn: false
    }
}

onSelectTrip = (rowData) => {
    selectedTrip = rowData;
}

renderRow(rowData) {
    return (
        <TouchableHighlight onPress={this.onSelectTrip(rowData)}  underlayColor='#dddddd'>
            <View style={styles.row}>
                <View style={{ paddingLeft: 5 }}>
                    <View>
                        <Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
                    </View>
                </View>
            </View>
        </TouchableHighlight>
    )
}

6 Comments

it appears it is trigging the function on page load. other than bunch of redlines underneath my code I am getting TypeError: Cannot read property 'setState' of undefined(…) !!!
Is the above code complete? I am not seeing any setState
no, i will create plunker with all of it. wasnt sure what all you needed.
Try changing the anonymous function for _.each in onSelectTrip method with a lambda.
it allowed the view to load without issue. but the function is not being triggered onPress
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.