Skip to content

A question about binding functions inside PureComponent? #16879

Description

@hoorsa

Is this a bug report?

No

Have you read the Contributing Guidelines?

Yes

Environment

OS: Windows 8
Node: 8.5.0
Yarn: Not Found
npm: 4.6.1
Watchman: Not Found
Xcode: N/A
Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed)
react-native: ^0.49.2 => 0.49.5
react: ^16.0.0-beta.5 => 16.0.0

Target Platform: Android (6.0)

Hi,
Goal is best performance. Which of below script result is better in performance (this is the renderItem PureComponent of a large FlatList):

1:

class MyComponent extends React.PureComponent {
constructor(props) {
super(props);
this.doWork = this.doWork.bind(this);
}
doWork() {
// doing some work here.
}
render() {
return <Text onPress={this.doWork}>Do Some Work</Text>
}
}

2:

class MyComponent extends React.PureComponent {
doWork = () => {
// doing some work here.
}
render() {
return <Text onPress={this.doWork}>Do Some Work</Text>
}
}

3:

class MyComponent extends React.PureComponent {
doWork(){
// doing some work here.
}
render() {
return <Text onPress={() => this.doWork}>Do Some Work</Text>
}
}

4:

class MyComponent extends React.PureComponent {
doWork(){
// doing some work here.
}
render() {
return <Text onPress={ this.doWork.bind(this) }>Do Some Work</Text>
}
}

5:
Your script...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions