реагировать-родной TouchableNativeFeedback onPress не работает - PullRequest
0 голосов
/ 27 мая 2018

Я создал составной компонент для создания TouchableNativeFeedback для wrapperComponent.

export default function withFeedback2(
    WrappedComponent
) {
    return class extends BaseComponent {
        constructor(props) {
            super(props);
        }

        render() {
            return (
                <View>
                    <TouchableNativeFeedback
                        onPress={() => this.props.onContainerViewPress()}

                    >
                        <WrappedComponent {...this.props} />
                    </TouchableNativeFeedback>
                    {/* <TouchableOpacity
                        onPress={this.props.onContainerViewPress ? () => this.props.onContainerViewPress() : null}

 >
                        <WrappedComponent {...this.props} />
                    </TouchableOpacity> */}
                </View>
            );
        }
    };
}

Но OnPress Событие TochableNativeFeedback не запускается.Принимая во внимание, что событие OnPress генерируется правильно, и onContainerViewPress опора оберточного компонента вызывается, если оберточный компонент обернут в TouchableOpacity.

Я проверяю это на платформе Android ..

Ответы [ 3 ]

0 голосов
/ 27 мая 2018

Используйте <View></View>, чтобы обернуть WrappedComponent для TouchableNativeFeedback.

<TouchableNativeFeedback
  onPress={() => this.props.onContainerViewPress()}>
    <View>
      <WrappedComponent {...this.props} />
    </View>
</TouchableNativeFeedback>
0 голосов
/ 12 декабря 2018

Я обнаружил, что добавление эффекта Ripple к TouchableNativeFeedback устраняет проблему для меня:

background={TouchableNativeFeedback.Ripple("#FFFFFF",true)}

т.е.

export default function withFeedback2(
  WrappedComponent
) {
  return class extends BaseComponent {
    constructor(props) {
      super(props);
    }

    render() {
      return (
        <View>
          <TouchableNativeFeedback
            onPress={() => this.props.onContainerViewPress()}
            background={TouchableNativeFeedback.Ripple("#FFFFFF",true)}
          >
            <WrappedComponent {...this.props} />
          </TouchableNativeFeedback>
        </View>
      );
    }
  };
}
0 голосов
/ 27 мая 2018

Вы можете вызвать метод, как показано ниже:

export default function withFeedback2(
    WrappedComponent
) {
    return class extends BaseComponent {
        constructor(props) {
            super(props);
            this.onContainerViewPress = this.onContainerViewPress.bind(this);

        }
          onContainerViewPress() {
        const { onContainerViewPress } = this.props;
        onContainerViewPress();
    }

        render() {
            return (
                <View>
                    <TouchableNativeFeedback
                                    onPress={() => { this.onContainerViewPress(); }}
                    >
                        <WrappedComponent {...this.props} />
                    </TouchableNativeFeedback>
                    {/* <TouchableOpacity
                        onPress={this.props.onContainerViewPress ? () => this.props.onContainerViewPress() : null}

 >
                        <WrappedComponent {...this.props} />
                    </TouchableOpacity> */}
                </View>
            );
        }
    };
}
...