reactjs - 如何使用 Props 初始化组件状态并对此状态进行一些更改?
问题描述
我有一个屏幕,它有一个
tags
用道具初始化的状态属性——道具是一个对象数组。设置后,我使用
this.state.tags
在 FlatList 中呈现对象项,然后通过移动它们并使用panResponder交换它们来与之交互。- 我的问题是在初始渲染显示的项目时,但我无法移动它们,但是当我更改代码以强制重新渲染时 - 然后我可以移动项目 - 我可能做错了什么。
class MyTags extends PureComponent<Props, State> {
props: Props;
state: State = {
tags: this.props.tags,
};
panResponder: PanResponder;
componentWillMount = async () => {
this.panResponder = this.createPanResponder();
await this.setState({tags: this.props.squadTemplate})
}
render() {
const {tags} = this.state
let myCards = [];
for (let i = 1; i < 5; i++) {
myCards.push({
id: i,
card: (
<CardHolder
tag={this.state.tags[i-1]}
/>
),
});
}
const slice1 = myCards.filter(card => card.card.props?.tag.new === true );
return (
<View style={styles.container}>
<FlatList
data={slice1}
renderItem={({item}) => (
<TouchableHighlight
style={{ marginHorizontal: 10, alignSelf: "center" }}
>
{item.card}
</TouchableHighlight>
)}
keyExtractor={item => item.id.toString()}
/>
</View>
)
解决方案
推荐阅读
- python-3.x - 当训练数据对 fit() 太大时,如何知道何时在 keras 中使用 fit_generator()?
- html - 我们可以在 AMP iframe 中添加非 AMP HTML 吗?
- python - 用列表理解替换字符串时出现 TypeError
- java - 匹配列表中的字符串并提取值
- sql - 如何从具有不同卫星的集线器中删除数据?
- javascript - 如何在我的应用程序中注入模板部分?
- acumatica - 设计器模式下 ListView 页面上的错误渲染控件(类型初始化程序 PX.Data.PXLicenseObserver 引发异常)
- c# - Docker 中长时间运行的 C# 进程
- android - 工具栏上后退按钮的自定义图像和颜色
- android - Android 屏幕截图视图旋转不出现