简体   繁体   English

反应带有数组道具错误更新的子组件

[英]React Child Component with Array Props Updating Incorrectly

My react web app uses Typescript. 我的React Web应用使用Typescript。

I'm having a problem with a child Component with array Props updating incorrectly when the array is updated in the parent with setState. 我在使用setState在父级中更新数组时,数组Props的子组件出现问题。

The child is declared in the parent Component render as follows (Alarms is the child Component, filteredStatus is the array): 子级在父级Component渲染中声明,如下所示(Alarms是子级Component,filteredStatus是数组):

<div key={1} className={'1'}>
  <span className='text'><b>{locationName} <i>Status</i></b></span>
  <Alarms filteredStatus={this.state.filteredStatus}/>
</div>

The child displays the array as a table. 子级将数组显示为表格。

public generate = () => {
    return this.props.filteredStatus.map(status =>
    {
      return (
      <TableRow key={status.location}>
        <TableCell align="left">
          {(status.severity === 40) && <ErrorIcon style={{ fill: '#FF3333'}}/>}
          {(status.severity === 30) && <NotificationIcon style={{ fill: 'FFA033'}}/>}
          {(status.severity === 20) && <WarningIcon style={{ fill: 'FFE033'}}/>}
        </TableCell>
        <TableCell align="left">{status.description}</TableCell>
        <TableCell align="left">{status.timestamp}</TableCell>
        <TableCell align="left">{status.location}</TableCell>

        {/* <div className="ListBtn">
          <Tooltip title={"Clear '" + alarm.who + "'"}>
            <Avatar>
              <IconButton aria-label="Delete" onClick={this.openClearAlarmConfirmDialog.bind(this, alarm)} >
                <DeleteIcon />
              </IconButton>
            </Avatar>
          </Tooltip>
        </div> */}
      </TableRow>
      );
    });
  }

  public render() {
    return (
      <div>
        {/* <Paper> */}
          <Table>
            <TableHead>
              <TableCell align="left">Severity</TableCell>
              <TableCell align="left">Description</TableCell>
              <TableCell align="left">Timestamp</TableCell>
              <TableCell align="left">Location</TableCell>
            </TableHead>
            <TableBody>
              {this.generate()}
            </TableBody>
          </Table>
        {/* </Paper> */}
      </div>
    );
  }


filteredStatus is declared in the parent Component state as...

    interface IState {
      dtName: string;
      dtDesc: string;
      dtInfo: IDrivetrainInfo;
      drillDownIndex: number;
      componentName: string;
      navLocations: string[];
      filteredStatus: Status[];
      nullStatus: Status[];
      locationStatus: Status;
    }

Status is a user defined class...


    export class Status {
        public location: string;
        public timestamp: string;
        public severity: number;
        public description: string;
    }

The filteredStatus is updated in the parent from a timer... A new array of Status is created and assigned to the state.filtered status as follows: 从计时器在父级中更新了filteredStatus ...创建了一个新的Status数组,并将其分配给state.filtered状态,如下所示:

this.setState({filteredStatus: this.localStatus});

What happens is that the updated table doesn't correctly show the new array of components. 发生的情况是,更新后的表未正确显示新的组件数组。 The new array appears to be merged with the old array. 新阵列似乎已与旧阵列合并。 I added the size of the array to the child display and at times the size shows 0 while the list shows 15 elements. 我将数组的大小添加到了子显示中,有时大小显示为0,而列表显示了15个元素。

I was able to work around this problem by first setting the filteredStatus to a null array, then the new array as follows: 通过首先将filteredStatus设置为空数组,然后将新数组设置为如下,我能够解决此问题:

const nullStatus = Array<Status>();
this.setState({filteredStatus: nullStatus});
this.setState({filteredStatus: this.localStatus});

I may be doing something wrong, but this appears to be a problem with the rendering of the array props in the child Component. 我可能做错了什么,但这在子Component中呈现数组props似乎是一个问题。

The table key... 表键...

<TableRow key={status.location}>

was changing based on the status location. 正在根据状态位置进行更改。 The table key must remain constant for the rendering to work correctly. 表键必须保持不变,渲染才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM