andreypopp / andreypopp/autobind-decorator
this.props is undefined on autobind decorator
- Ngôn ngữ chính
- JavaScript
- Star
- 1.4k
- Fork
- 65
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Đánh giá
Issue này chưa được đánh giá.