如何在这些代码中执行正确的useState?

问题描述 投票:0回答:1

我在渲染方面遇到问题,因此有人告诉我将用作状态的表设置为可正确更新数据的状态。

我正在使用的表:

<ScrollView horizontal={true} style={{ marginTop: 20 }}>
  <View>
    <Table borderStyle={{ borderWidth: 1, borderColor: '#C1C0B9' }}>
      <Row
        data={this.state.tableHead}
        widthArr={this.state.widthArr}
        style={styles.header}
        textStyle={styles.text}
      />
    </Table>
    <ScrollView style={styles.dataWrapper}>
      <Table borderStyle={{ borderWidth: 1, borderColor: '#C1C0B9' }}>
        {this.state.tableData.map((rowData, index) => (
          <Row
            key={index}
            data={rowData}
            widthArr={this.state.widthArr}
            style={[styles.row, index % 2 && { backgroundColor: '#F7F6E7' }]}
            textStyle={styles.text}
          />
        ))
        //this.state.myState
        }
      </Table>
    </ScrollView>
  </View>
</ScrollView>

它在render()下工作,我尝试制作一个setState onPress选项来激活它,我做了类似的事情:

updateState = () =>
  this.setState({
    myState: this.state.tableData.map((rowData, index) => (
      <Row
        key={index}
        data={rowData}
        widthArr={this.state.widthArr}
        style={[styles.row, index % 2 && { backgroundColor: '#F7F6E7' }]}
        textStyle={styles.text}
      />
    )),
  })

但是无法正常工作。

reactjs react-native
1个回答
1
投票

添加toggle状态。此状态将在每个onPress事件上更改。

((对于下面的示例,我正在使用onClick

© www.soinside.com 2019 - 2024. All rights reserved.