如何在React Native中更改ListView中按钮的文本? [英] how to change text of button in listview in react native?

查看:98
本文介绍了如何在React Native中更改ListView中按钮的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击后,我试图快速更改列表视图中的按钮的文本吗?

I am trying to change text of button which is in listview quickly after click on it?

我正在尝试使用以下代码来实现此目的,但我无法做到这一点,即它无法正常工作.我该怎么做?请帮忙.

I am trying with following code to implement this but i cant do it i.e its not working. How can i do it? please help.

constructor(props) {
    super(props);
    this.state = {
      button_text:"Connect",
    }
  }

  ConPressed() {
    this.setState({
      button_text: "Connected",
    });
  }

  render() {
    return (
      <ListView
        dataSource={this.state.sa}
        renderRow={(rowData) => 

       <TouchableHighlight onPress={() => this.ConPressed()}>
          <Text>{this.state.button_text}</Text>
       </TouchableHighlight>

      />
    );
  }

推荐答案

export default class ListItem extends Component {
  constructor(props) {
    super(props);

    this.state = {
      button_text: 'Connect',
    }
  }


  ConPressed = () => {
    this.setState({ button_text: 'Connected' });
  }


  render() {
    return (
      <TouchableHighlight onPress={this.ConPressed}>
        <Text>{this.state.button_text}</Text>
      </TouchableHighlight>
    );
  }
}

因此,现在您要在原始文件中导入ListItem,并在renderRow中使用它.

So now you want to import your ListItem in your original file, and use that in your renderRow.

renderRow={() => <ListItem />}

这篇关于如何在React Native中更改ListView中按钮的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆