andreypopp / andreypopp/autobind-decorator

this.props is undefined on autobind decorator

Offen
#84 2 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
1.4k
Forks
65
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I've got some problems while using the autobind decorator on React Native.

I use the newest release v2.4.
react-native: 0.54.0

Example:

I've implemented two Screens:

First with button to Navigate to the second screen.

Second screen is rendering a component e.g. a customized TouchableOpacity (CustomTouchableOpacity).

The CustomTouchableOpacity got an onPress prop and implements a function that is calling `this.props.onPress` from the ReactNative given `onPress` function of `TouchableOpacity`.

After multiple Transitions (navigate with ReactNavigation) between the First and Second Screen, `this.props` of the `CustomTouchableOpacity` is undefined.

```
import autobind from 'autobind-decorator';
import React from 'react';
import { View } from 'react-native';
import CustomTouchableOpacity from '../components/CustomTouchableOpacity';

class SecondScreen extends React.Component {
constructor (props) {
super(props);
}

@autobind
onPress () {
// do some stuff and navigate back (pop)
}

render () {
return (



)
}
}

module.exports = SecondScreen;
```

```
// CustomTouchableOpacity
import autobind from 'autobind-decorator';
import React from 'react';
import { TouchableOpacity } from 'react-native';
import { Text } from 'react-native';

class CustomTouchableOpacity extends React.Component {
constructor (props) {
super(props);
}

@autobind
onPress () {
if (this.props) {
this.props.onPress();
} else {
alert('empty props');
}
}

render () {
return (

{this.props.label}

);
}
}

module.exports = CustomTouchableOpacity;
```
This problem only exists if im NOT connected to the Remote Debugger.

On using the traditional `this.onPress = this.onPress.bind(this)` binding the component never loses the props.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.