Column filter doesn’t work on React table.

huangapple go评论74阅读模式
英文:

Column filter doesn't work on React table

问题

I'm working on react table and with custom filter.

In my case I have filter component and react table component which are sibling component, when user select a value from dropdown in filter component and that value is of particular column which is received using useSelector(redux toolkit) in react table but filtering is not working.

Here is code block of filter component

const ColumnFilter = ({ column }) => {
  const { filterValue, setFilter } = column;
  const { filterSliceData } = useSelector(getDataSlice);

  if (filterSliceData) {
    setFilter(filterSliceData);
  }

  return (
    <></>
  );
};

I'm getting some error when I select filter from dropdown.

And Here is the Full example that I have created in codesandbox. Link to CodeSandbox

英文:

I'm working on react table and with custom filter.

In my case I have filter component and react table component which are sibling component, when user select a value from dropdown in filter component and that value is of particular column which is received using useSelector(redux toolkit) in react table but filtering is not working.

Here is code block of filter component

const ColumnFilter = ({ column }) =&gt; {
  const { filterValue, setFilter } = column;
  const { filterSliceData } = useSelector(getDataSlice);

  if (filterSliceData) {
    setFilter(filterSliceData);
  }

  return (
    &lt;&gt;&lt;/&gt;
  );
};

I'm getting some error when I select filter from dropdown.
Column filter doesn’t work on React table.

And Here is the Full example that I have created in codesandbox. https://codesandbox.io/s/react-table-column-filter-component-c5wbc7

答案1

得分: 1

ColumnFilter.js文件中的逻辑导致了无限重新渲染循环,因为它不断引发状态更改,从而触发ColumnFilter的重新渲染。 filterSliceData 是真值,这告诉它调用 setFilter,从而引发列的重新渲染,重新渲染ColumnFilter,其中 filterSliceData 是真值,依此类推。

听起来你想要的是一种方法,如果外部组件发生变化,可以更改筛选值。useFilter 钩子 公开了一个 setFilter 方法,你可以使用这个方法来实现。然后,你可以使用useEffect或类似的钩子来基于来自useSelector的值更新所需列的筛选值。我分叉了你的 CodeSandbox 作为示例。这种方法的主要缺点是它将特定的全局状态变量与表格本身紧密关联,因此可能需要一些额外的逻辑来为你期望的结果提供抽象。

英文:

Your logic in ColumnFilter.js is causing an infinite re-render loop because it keeps causing a state change that prompts a re-render of the ColumnFilter. filterSliceData is truthy, which tells it to call setFilter, which prompts a column rerender, which re-renders ColumnFilter, where filterSliceData is truthy, and so on and so forth.

What it sounds like you want is a way to change your filter value if an external component changes. The useFilter hook exposes a setFilter method you can use for this. You can then use that setFilter method to update the filter value for your desired column based on the value from useSelector, with a useEffect or similar hook. I forked your CodeSandbox as an example. The primary downside to this approach is that it's tightly links that specific global state variable with the table itself, so some additional logic might be needed to abstract that out for your desired result.

huangapple
  • 本文由 发表于 2023年2月27日 17:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定