Error: Invariant Violation: Touchable child must either be native or forward setNativeProps to a native component stack

Error: Invariant Violation: Touchable child must either be native or forward setNativeProps to a native component stack

我很难处理这个错误。

我不明白为什么?我有一个未使用自定义组件的子组件。我将 TextImageIcon 包装在 View 组件中,然后将它们包装在 TouchableHighlight 中仍然在特定页面上出现错误,但在其他页面上没有做同样事情的页面。

    <TouchableHighlight
      onPress={this.changeTo} style={PictureStyles.btnClickContain}
      underlayColor='#042417'>
      <View
        style={PictureStyles.btnContainer}>
        <Icon
          name='fontawesome|calendar'
          size={25}
          color='#042'
          style={PictureStyles.btnIcon}/>
        <Text style={PictureStyles.btnText}>Book</Text>
      </View>
    </TouchableHighlight>

按照文档中有关 Composite components and setNativePropssetNativeProps 转发到 child 的说明,我能够修复我的应用程序中的类似错误。来自文档:

All we need to do is provide a setNativeProps method on our component that calls setNativeProps on the appropriate child with the given arguments.

下面是您的代码,更新了与我相同的更改。我也在使用 react-native-icons,因为看起来你也是。

唯一的变化是 setNativeProps 方法和使用 ref callback syntax

View 上创建 ref
var MyComponent = React.createClass({
  setNativeProps (nativeProps) {
    this._root.setNativeProps(nativeProps);
  }

  render () {
    return (
      <TouchableHighlight
        onPress={this.changeTo} style={PictureStyles.btnClickContain}
        underlayColor='#042417'>
        <View
          ref={component => this._root = component}
          style={PictureStyles.btnContainer}>
          <Icon
            name='fontawesome|calendar'
            size={25}
            color='#042'
            style={PictureStyles.btnIcon}/>
          <Text style={PictureStyles.btnText}>Book</Text>
        </View>
      </TouchableHighlight>
    );
  }
})