简体   繁体   English

在对象数组中添加/删除设置/存储在 state 中的列时,功能组件数据表不重新呈现

[英]Functional component data table not rerendering when add/remove columns which is set/stored in state in an array of objects

I am building a datatable and have added a checkbox dropdown for adding/removing columns in the table.我正在构建一个数据表,并添加了一个复选框下拉列表,用于添加/删除表中的列。 Everything works fine except it is not rerendering the table until I click on one of the header titles.一切正常,只是在我单击 header 标题之一之前它不会重新渲染表格。

At the top of the function component, I am setting the state as follows:在 function 组件的顶部,我将 state 设置如下:

  const [formColumns, setDisplayFields] = useState(formFields);

formFields is retrieved from a file and is formatted as below. formFields 是从文件中检索出来的,格式如下。 It's an array of JSON column objects that looks like this:这是一个 JSON 列对象的数组,如下所示:

    [id: {
      name: 'id',
      label: 'Patient Id',
      displayColumn: false,
      displayOrder: 1,
    },
    firstName: {
      name: 'firstName',
      label: 'First Name*',
      requiredErrorMsg: 'First name is required',
      displayColumn: true,
      displayOrder: 2,
    },
    middleName: {
      name: 'middleName',
      label: 'Middle Name',
      displayColumn: false,
      displayOrder: 0,
    },
    lastName: {
      name: 'lastName',
      label: 'Last Name*',
      requiredErrorMsg: 'Last name is required',
      displayColumn: true,
      displayOrder: 0,
    },
    suffix: {
      name: 'suffix',
      label: 'Suffix',
      displayColumn: false,
      displayOrder: 0,
    },
    dob: {
      name: 'dob',
      label: 'Date of Birth*',
      requiredErrorMsg: 'Birth date is required',
      invalidErrorMsg: 'This is not a valid date',
      displayColumn: true,
      displayOrder: 3,
    },
    organization: {
      name: 'organization',
      label: 'Organization',
      displayColumn: false,
      displayOrder: 4,
    },
  ]

There are actually more columns, but this should suffice for describing what I'm doing.实际上有更多的列,但这足以描述我在做什么。 The 'displayColumn' element is changed in an onChange event in the same component that displays the table (passes it as a prop into checkbox menu). 'displayColumn' 元素在显示表格的同一组件中的 onChange 事件中发生更改(将其作为道具传递到复选框菜单中)。 Here's the onchange:这是onchange:

  /**
   * handleCheckboxSelect
   * @param {string} colname
   */
  var handleCheckboxSelect = (colname) =>
  {
    //return the index of the column checked/unchecked
    var icol = getColumnIndex(formColumns, colname);
    console.log('FOUND COLINDEX = ' + icol + '.');
    if (icol > -1) {
      //toggle the display parameter
      formColumns[icol].displayColumn = (formColumns[icol].displayColumn === false);
      //store the updated column in the column list
      setDisplayFields(formColumns);
    }
    else {
      console.log('checkbox colname = ' + colname);
    }
  }

As you can see, I am using state as required, and any time state changes, it should force a re-render.如您所见,我根据需要使用 state ,并且任何时候 state 更改,它都应该强制重新渲染。 However, it's not.然而,事实并非如此。 Is it because I am using an array of objects?是因为我使用了一个对象数组吗? If I pass a single column back and stroe that in state, would that fix the issue?如果我将单列返回并在 state 中传递,那会解决问题吗? I'm stumped.我难住了。 Any help would be greatly appreciated!任何帮助将不胜感激!

You are setting same formColumns reference on setDisplayFields which will not trigger re-render of the component.您正在setDisplayFields上设置相同的formColumns引用,这不会触发组件的重新渲染。 Try to copy formColumns to a new array and make changes on that array.尝试将formColumns复制到一个新数组并对该数组进行更改。

Try this -尝试这个 -

var handleCheckboxSelect = (colname) =>
  {
    //create new array from existing one
    let newColumns = [...formColumns];

    var icol = getColumnIndex(newColumns, colname);
    console.log('FOUND COLINDEX = ' + icol + '.');
    if (icol > -1) {
      //toggle the display parameter
      newColumns[icol].displayColumn = (newColumns[icol].displayColumn === false);
      //store the updated column in the column list
      setDisplayFields(newColumns);
    }
    else {
      console.log('checkbox colname = ' + colname);
    }
  }

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

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