react-table lost focus on filter?

两盒软妹~` 提交于 2021-02-09 11:47:06

问题


I am making a table base on react-table v7 and react-window. I found other question on this site with the same question but almost they are using v6 - almost difference.

Problem is the filter text field will lost focus after we type and even when we just touch the input (I know the table is re-render). But I cannot find any solution for days.

Please help and thank you very much.

Here is the code sanbox


回答1:


You are using uniqied() for key i.e key={uniqid()}. So on every re-render, you are creating a new id and you are assigning it to key. When key prop changes, the component are not re-rendered, but it is re-mounted. Therefore focus is lost.

Below should work.

<div {...getTableProps()} className={css.table} style={{ ...styles }}>
          <div className={css.thead}>
            {headerGroups.map((headerGroup, index) => (
              <div
                key={index}
                {...headerGroup.getHeaderGroupProps()}
                className={css.heading + " " + css.tr}
              >
                {headerGroup.headers.map((column, i) => (
                  <div
                    key={i}
                    className={
                      css.th + " " + (column.fixedWidth ? css.fixed__width : "")
                    }
                    style={{ ...getCellStyles(column) }}
                  >
                    <div className={css.th__inner}>
                      <div {...column.getHeaderProps()}>
                        <div

Working copy of code is here:

https://codesandbox.io/s/table-key-issue-bpkly

Quick note - In the above code, you can use a unique value that remains constant throughout the render cycle. For demo sake, I have used index.



来源:https://stackoverflow.com/questions/61201030/react-table-lost-focus-on-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!