Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get 'this' in <ListView> renderRow function #967

Closed
ReadingSteiner opened this issue Apr 22, 2015 · 4 comments
Closed

Cannot get 'this' in <ListView> renderRow function #967

ReadingSteiner opened this issue Apr 22, 2015 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@ReadingSteiner
Copy link

I wrote a function called renderMyRow to render rows in a ListView, but I found it is strange that I can't get this in this function.

Then I fixed it by add a bind(this) in my code, but i wonder why i cannot get 'this' in my renderMyRow ?

here is the code

class SearchResult extends Component {

    constructor(props) {
        super(props);
        var dataSource = new ListView.DataSource({
            rowHasChanged: (r1,r2) => r1.guid !== r2.guid
        });
        this.state = {
            dataSource: dataSource.cloneWithRows(this.props.listings)
        };
    }

    render() {
        return (
            <ListView dataSource={this.state.dataSource} renderRow={this.renderMyRow} />
        )
    }

    rowPressed(propertyGuid) {
        var property = this.props.listings.filter(prop => prop.guid === propertyGuid)[0];

        this.props.navigator.push({
            title: 'Property',
            component: PropertyView,
            props: {property: property}
        })
    }

    renderMyRow(rowData, sectionID, rowID) {
        console.log(this); // Output: null
        var price = rowData.price_formatted.split(' ')[0];
        return(
            <TouchableHighlight underlayColor='#DDDDDD' onPress={ () => this.rowPressed(rowData.guid) }>
                <View>
                    <View style={styles.rowContainer}>
                        <Image style={styles.thumb} source={{uri : rowData.img_url}}/>
                        <View style={styles.textContainer}>
                            <Text style={styles.price}>£{price}</Text>
                            <Text numberOfLines={1}>{rowData.title}</Text>
                        </View>
                    </View>
                    <View style={styles.separator}/>
                </View>
            </TouchableHighlight>
        );
    }
}
@gabro
Copy link
Contributor

gabro commented Apr 22, 2015

This is intended behavior when using ES6 classes. See https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding

tl; dr

ES6 Component class does not autobind this like React.createClass does.

@ReadingSteiner
Copy link
Author

Thanks a lot!

@jibebec
Copy link

jibebec commented Sep 29, 2016

Hello, Iam very newbie with react-native and even React framework. I have the same issue with Renderrow and this...
I read the doc that you gave but i wonder how i can make the onPress call work without this.onPress.
Thank you for your Help!

@rupamgho123
Copy link

rupamgho123 commented Jan 30, 2018

Add this.renderMyRow = this.renderMyRow.bind(this); in constructor

@facebook facebook locked as resolved and limited conversation to collaborators May 29, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants