Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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
Reactjs 如何在这些代码中执行正确的useState?_Reactjs_React Native - Fatal编程技术网

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

Reactjs 如何在这些代码中执行正确的useState?,reactjs,react-native,Reactjs,React Native,我对渲染有问题,所以我被告知将我使用的表设置为状态,以便数据可以正确更新 我正在使用的表: <ScrollView horizontal={true} style={{ marginTop: 20 }}> <View> <Table borderStyle={{ borderWidth: 1, borderColor: '#C1C0B9' }}> <Row data={this.state.tableHead}

我对渲染有问题,所以我被告知将我使用的表设置为状态,以便数据可以正确更新

我正在使用的表:

<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>

但无法使其工作。

添加一个
切换状态。此状态将在每次按下
onPress
事件时更改

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

const{useState}=React;
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
切换:false,
};
数据=[1,2,3,4];
}
render(){
返回(
this.setState({toggle:!this.state.toggle})
}>
{this.state.toggle?'hide':'show'}
{this.state.toggle?this.data.map(d=>(
{d}
)):null}
);
}
}
ReactDOM.render(
,
document.getElementById('root'))
);


您想设置什么状态?你的状态“myState”包含什么?myState是从scrollView开始尝试保存所有表,但它只允许我使用一件事,所以我尝试保留map函数。它用我从db中选取的一些数据绘制了一个表格,但仅在我更改状态或再次单击按钮时才更新。鉴于使用了
此选项,P绝对不使用功能组件。状态
您应该更新答案,以给出类组件的解决方案,但我的状态应该是什么?或者我只是切换作为?@Try2prog的“{}”,这个想法是使用
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}
      />
    )),
  })