首页 > 解决方案 > 如何使用 Props 初始化组件状态并对此状态进行一些更改?

问题描述


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


标签: reactjsreact-native

解决方案


推荐阅读