如何在react native中更改listview中按钮的文本?

如何在react native中更改listview中按钮的文本?,listview,react-native,react-native-listview,Listview,React Native,React Native Listview,我试图改变按钮的文本,它是在listview快速点击后 我试图用以下代码来实现这一点,但我不能这样做,即它不工作。我怎么做?请帮忙 constructor(props) { super(props); this.state = { button_text:"Connect", } } ConPressed() { this.setState({ button_text: "Connected", }); } re

我试图改变按钮的文本,它是在listview快速点击后

我试图用以下代码来实现这一点,但我不能这样做,即它不工作。我怎么做?请帮忙

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>

      />
    );
  }
构造函数(道具){
超级(道具);
此.state={
按钮文本:“连接”,
}
}
ConPressed(){
这是我的国家({
按钮文本:“已连接”,
});
}
render(){
返回(
this.ConPressed()}>
{this.state.button_text}
/>
);
}
ConPressed()
绑定到范围

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

在touchable的代码中,传递的是方法执行,而不是方法引用

<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>
    );
  }
}
建造师(道具){ 超级(道具); 此.state={ 按钮_文本:“连接”, } } ConPressed=()=>{ this.setState({button_text:'Connected'}); } render(){ 返回( {this.state.button_text} ); } }
因此,现在您希望在原始文件中导入ListItem,并在
渲染中使用它


renderRow={()=>}

谢谢您的回复。但是它不起作用,它显示错误
TypeError:undefined不是对象(计算'\u this.Conpressed.bind')
很抱歉我漏掉了一个字符this.Conpressed=this.Con“P”ressed.bind(this);:)是的,我做到了。但按下后,它也不工作,显示的文本与
connect
相同。哪里出了问题?你能给这样的在线零食博览会
https://snack.expo.io/SywBhpMgW
这对我很有帮助…我发现问题很明显,当一行已经渲染时,你无法更新它的道具,你需要创建一个新的对象你能确认ConPressed在按下按钮时被调用吗?是的,我使用的和你在回答中给出的相同,它在
TouchableHighlight
上被调用,即在按下按钮时被调用。@mayurespatil没问题:)@MattyK14在不按TouchableOpacity的情况下调用onPress方法有问题。@MattyK14在不按TouchableOpacity的情况下被调用,在屏幕初始化时被调用。@Rushitrivedi您有胖箭头函数:
=()=>
?如果没有,您是否绑定(此)
this.ConPressed = this.ConPressed.bind(this)
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>
    );
  }
}