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}
/>
)),
})