Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android React本机ListView renderRow问题传递道具。正确的方法还是错误的方法_Android_Ios_Reactjs_React Native_Redux - Fatal编程技术网

Android React本机ListView renderRow问题传递道具。正确的方法还是错误的方法

Android React本机ListView renderRow问题传递道具。正确的方法还是错误的方法,android,ios,reactjs,react-native,redux,Android,Ios,Reactjs,React Native,Redux,在React Native中使用ListView时,我看到了不同之处,将道具移动到列表项 将函数作为道具仅与引用一起传递,并调用子组件中的参数,或 将函数作为已定义参数的道具传递,并在子函数中不包含参数的情况下调用该函数 这些解决方案都不管用 调用的函数是Redux的操作创建者,并已调度。这是Redux或React-Native(可能是React-JS)的问题吗 这是一段代码片段//ERROR代码行不起作用,后面是好代码行 class App extends Component { //

在React Native中使用ListView时,我看到了不同之处,将道具移动到列表项

  • 将函数作为道具仅与引用一起传递,并调用子组件中的参数,或

  • 将函数作为已定义参数的道具传递,并在子函数中不包含参数的情况下调用该函数

  • 这些解决方案都不管用

    调用的函数是Redux的操作创建者,并已调度。这是Redux或React-Native(可能是React-JS)的问题吗

    这是一段代码片段//ERROR代码行不起作用,后面是好代码行

    class App extends Component {
    
      // On props 
      // data: an Array
      // doThis: an action creator of Redux
      // doThat: idem
    
      constructor(){
        super();
        this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
      }
    
      render () {
        const dataSource = this.ds.cloneWithRows(this.props.data);
    
        return (
          <View>
            <ListView style={{flex:1}}
                dataSource={dataSource}
                renderRow={(rowData, sectionID, rowID) =>
                  <Item  rowData={rowData}
                  //ERROR
                  //onPress={this.props.doThis}
                  //onLongPress={this..props.doThat}
                  //RIGHT NO ERROR TOO
                  onPress={() => this.props.doThis(rowData)}
                  onLongPress={() => this.props.doThat(rowData)}
                  />
                }
              />
          </View>
        )
      }
    }
    
    class Item extends Component {
      render() {
        return (
          <View>
            <TouchableHighlight
              //ERROR 
              //onPress={() => { this.props.onPress( this.props.rowData ) }}
              //onLongPress={() => { this.props.onLongPress( this.props.rowData ) }}
              //WRONG TOO
              onPress={this.props.onPress}
              onLongPress={this.props.onLongPress}
              >
              <Text>
                {rowData}
              </Text>
            </TouchableHighlight>
          </View>
        );
      }
    }
    
    类应用程序扩展组件{
    //论道具
    //数据:数组
    //多蒂斯:Redux的动作创造者
    //多蒂:同上
    构造函数(){
    超级();
    this.ds=新的ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
    }
    渲染(){
    const dataSource=this.ds.cloneWithRows(this.props.data);
    返回(
    this.props.doThis(rowData)}
    onLongPress={()=>this.props.doThat(rowData)}
    />
    }
    />
    )
    }
    }
    类项扩展组件{
    render(){
    返回(
    {this.props.onPress(this.props.rowData)}
    //onLongPress={()=>{this.props.onLongPress(this.props.rowData)}
    //也错了
    onPress={this.props.onPress}
    onLongPress={this.props.onLongPress}
    >
    {rowData}
    );
    }
    }
    
    这里有一个关于这个问题的回购协议
    提前感谢

    这可能是您的
    在结束语中设置不正确的问题

    尝试以这种方式绑定它:

    class Item extends Component {
      render() {
        return (
          <View>
            <TouchableHighlight
              onPress={this.props.onPress.bind(this, this.props.rowData)}
              onLongPress={this.props.onLongPress.bind(this, this.props.rowData)}
              >
              <Text>
                {rowData}
              </Text>
            </TouchableHighlight>
          </View>
        );
      }
    }
    
    类项扩展组件{
    render(){
    返回(
    {rowData}
    );
    }
    }
    
    如果高级回调接受参数,则需要确保匿名函数也接受参数(注意:使用箭头语法创建匿名函数会自动将函数绑定到当前上下文中的
    this
    的值)。我认为您看到了一系列问题,您的回调绑定到了不正确的上下文(窗口),或者您不接受传递的参数:

    class App extends Component {
    
      // On props 
      // data: an Array
      // doThis: an action creator of Redux
      // doThat: idem
    
      constructor(){
        super();
        this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
      }
    
      render () {
        const dataSource = this.ds.cloneWithRows(this.props.data);
    
        return (
          <View>
            <ListView style={{flex:1}}
                dataSource={dataSource}
                renderRow={(rowData, sectionID, rowID) => 
                  <Item rowData={rowData}
                      onPress={(data) => this.props.doThis(data)}
                      onLongPress={(data) => this.props.doThat(data)} />
                }/>
          </View>
        )
      }
    }
    
    class Item extends Component {
      render() {
        return (
          <View>
            <TouchableHighlight
              onPress={() => this.props.onPress(this.rowData)}
              onLongPress={() => this.props.onLongPress(this.rowData)}>
              <Text>
                {rowData}
              </Text>
            </TouchableHighlight>
          </View>
        );
      }
    }
    
    类应用程序扩展组件{
    //论道具
    //数据:数组
    //多蒂斯:Redux的动作创造者
    //多蒂:同上
    构造函数(){
    超级();
    this.ds=新的ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
    }
    渲染(){
    const dataSource=this.ds.cloneWithRows(this.props.data);
    返回(
    this.props.doThis(数据)}
    onLongPress={(数据)=>this.props.doThat(数据)}/>
    }/>
    )
    }
    }
    类项扩展组件{
    render(){
    返回(
    this.props.onPress(this.rowData)}
    onLongPress={()=>this.props.onLongPress(this.rowData)}>
    {rowData}
    );
    }
    }
    
    Hi@Moti Azu,我真的不知道我和你的答案有什么不对。两者都错了。我在想,在renderRow和Item上传递callbak的正确方法是调用函数。但是不起作用。这是要测试的回购协议,我想你可以帮助我:Hi@Moti Azu为了避免这里的长时间讨论(不喜欢评论员),我删除了我的评论。详细的解释在回购协议上。单击主组件上的按钮可以正常工作(列表上的添加、删除、更新等),但单击列表上的项目不会产生相同的行为,然后从单击的项目中删除所有项目。所需的行为是更改项目的状态,但状态会更改并删除其余项目。对不起,我的英语很糟糕。谢谢@calvin belden,我尝试了你的方法,但结果是一样的。我在ListView中的renderRow上编码了'onPress={(rowID)=>{this.props.update(rowID)}}}`并在Item上编码了
    onPress={()=>this.props.onPress(rowID)}
    ,项目发生了变化,但剩余的项目被删除了。rowID是否在Item组件的
    render
    函数中定义了?您能否设置断点(或以某种方式调试)以查看父处理程序是否被调用。完成!正确的方法是在renderRow上使用
    onPress={()=>this.props.update(parseInt(rowID))}
    在Item上使用
    onPress={this.props.onPress}
    。但重要的是回购协议中的
    rowID
    parseInt
    。rowID是字符串而不是数字!。然后减速机中的函数(Redux)总是转换为0,然后全部错误!太谢谢你们了!我很好奇,除了箭头符号之外,还有没有其他方法?因此,与
    onPress={(data)=>this.props.doThis(data)
    onPress={this.props.doThis.bind(this,data)类似的东西的等价物是什么
    ?或类似的东西?您不会绑定数据参数,因为绑定时不知道该参数;
    数据在触发事件时会传递给事件侦听器。但除此之外[功能上]没有区别。使用Function.prototype.bind会影响性能,我还发现箭头语法更简洁。