javascript - 从被按下的平面列表项导航到另一个屏幕

标签 javascript react-native react-redux wix-react-native-navigation

我一直在使用 wix/react-native-navigation 包在屏幕之间导航并正确处理堆栈。

跨屏幕移动非常简单,按下按钮时会触发这些转换。但是,当我有一个 FlatList 并且我想在用户点击列表中的项目时推送到新屏幕时,问题就出现了,看起来开始时注入(inject)的导航器 Prop 丢失了,或者处于 onPress 回调事件之外的另一个上下文中;

这是示例代码

class AlertType extends React.PureComponent {
  _onPress = () => {
      this.props.onPressItem(this.props.itemId, this.props.itemName, this.props.itemImageUrl);
  };

  render() {
      return (
          <TouchableOpacity { ...this.props }
              onPress={ this._onPress }
              style={ itemStyle.cardContainer }>

              <View style={ itemStyle.mainContainer }>
                  <View style={{ width: 10 }}/>
                  <Image
                      source={{ uri: NET.HOST + this.props.itemImageUrl }}
                      style={{ width: 45, height: 45 }}
                  />
                  <View style={{ width: 10 }}/>
                  <Text style={ itemStyle.itemText }>{ this.props.itemName }</Text>
              </View>
          </TouchableOpacity>
      );
  }
}

class AlertsScreen extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            alertTypes: null,
        }
    }

    _onAlertTypePressed(typeId: string, typeName: string, imageUrl: string){

        this.props.navigator.push({
            screen: 'prod.screens.AlertsCreator',
            title: 'Alert',
            passProps: {
                alertId: typeId,
                alertName: typeName,
                alertImage: imageUrl
            }
        });
    }

    _renderListItem = ({ item }) => (
        <AlertType
            itemName={ item.titulo }
            itemId={ item.key }
            itemImageUrl={ item.url }
            onPressItem={ this._onAlertTypePressed }
        />
    );

    render() {
        return (
            <View style={ styles.mainContainer }>
                <FlatList
                    data={ this.state.alertTypes }
                    ItemSeparatorComponent={ () => <View style={{ height: 5 }}/> }
                    renderItem={ this._renderListItem }
                />
            </View>
        );
    }

const mapSessionStateToProps = (state, ownProps) => {
    return {
        session: state.session
    };
}
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    };
}
export default connect(mapSessionStateToProps, mapDispatchToProps)(AlertsScreen)

这种方法会产生下一个错误 enter image description here

一定是我遗漏了一些东西,我知道 this.props.navigator 不是未定义的,但在 _onAlertTypePressed 内部 navigator 属性未定义。

最佳答案

问题在于您将函数传递给组件而没有将其绑定(bind)到当前上下文。

你应该通过:

this._onAlertTypePressed.bind(this);

另一种方法是在构造函数中绑定(bind)函数:

constructor(props) {
  this._onAlertTypePressed = this._onAlertTypePressed.bind(this);
}

关于javascript - 从被按下的平面列表项导航到另一个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459247/

相关文章:

javascript - 当用户在 Kendo 自动完成中按退格键时防止回发

javascript - 如何按倍数数组/对象值过滤对象数组

javascript - 声明变量比 setState 更快

javascript - 未找到模块 : Can't resolve '@react-navigation/stack' React-Native

javascript - 为什么文档中的 auth0 redux 示例会在构造函数中抛出 null 错误?

reactjs - 如何将 React 16.3 上下文提供程序与 redux 存储一起使用?

php - Symfony Javascript 表单验证

javascript - JS Marko(猛禽模板)在浏览器中加载动态模板路径

javascript - 发送 2 个参数来 React Redux 中间件,而不是只发送一个操作

reactjs - React(w/react-redux)缓存选项