React-table: How to update state.hiddenColumn=[]?

Created on 3 Feb 2020  路  8Comments  路  Source: tannerlinsley/react-table

Hi, I have an external function to trigger state.hiddenColumn provided in react table v7 using useEffect. The table and hidden columns is not rerender after I pass new props hiddenColumn. How to update state.hiddenColumn array properly. I have read the documentation, but not any example real code include.

Piece of my code

component 1

export default function Project(props: any) {
  const [value, setValue] = React.useState<Array<string>>([]);

  const hide1 = () => {
    const hiddenColumn= ["status", "timeline"];
    setValue(hiddenColumn);
  };

  return (
    <div className="view-project-wrapper">
          <button onClick={hide1}>test 1</button>
          <Table  columns={columns}
          data={data}  hiddenColumns={value} />
    </div>
  );
}

component 2

function Table({ columns, data, hiddenColumns = [] }) {
  console.log("table");
  console.log(hiddenColumns);
  // Use the state and functions returned from useTable to build your UI
  const { getTableProps, headerGroups, rows, prepareRow } = useTable(
    {
      columns,
      data,
      initialState: {
        hiddenColumns: hiddenColumns
      }
    },
    useFlexLayout,
    // useBlockLayout,
    useResizeColumns,
    useSticky
  );

  React.useEffect(() => {
    console.log("useEffect");
  });

  // Render the UI for your table
  return (.....rest)
}

Most helpful comment

Well that's one way, but it looks like you're trying to fit a React lifecycle method into a React effect hook. Using the following piece of code would achieve the same in the way it's meant to be used.

React.useEffect(() => {
  setHiddenColumns(hiddenColumns)
}, [hiddenColumns])

All 8 comments

Have a look at this example: https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/column-hiding

I have look this example before I ask here. As i said, I have external function (customized function) to pass list of hiding columns props into react table. Before that I think column.show is working fine, but it's deprecated.

Then have a look at this hook: https://github.com/tannerlinsley/react-table/blob/master/src/hooks/useColumnVisibility.js

As you can see there are a couple of functions passed into the instance with which you can control the column visibility.

Then have a look at this hook: https://github.com/tannerlinsley/react-table/blob/master/src/hooks/useColumnVisibility.js

As you can see there are a couple of functions passed into the instance with which you can control the column visibility.

Thanks for reply, I don't know how to use columnvisibility. But, I have found the solution using setHiddenColumns()

let prevHiddenColumns = [];
function Table({ columns, data, hiddenColumns = [] }) {
  // Use the state and functions returned from useTable to build your UI
  const {
    getTableProps,
    headerGroups,
    rows,
    prepareRow,
    state,
    setHiddenColumns
  } = useTable(
    {
      columns,
      data,
      initialState: {
        hiddenColumns: hiddenColumns
      }
    },
    useFlexLayout,
    // useBlockLayout,
    useResizeColumns,
    useSticky
  );

  React.useEffect(() => {
    if (JSON.stringify(prevHiddenColumns) != JSON.stringify(hiddenColumns)) {
      prevHiddenColumns = hiddenColumns;
      setHiddenColumns(hiddenColumns);
    }
  });

Well that's one way, but it looks like you're trying to fit a React lifecycle method into a React effect hook. Using the following piece of code would achieve the same in the way it's meant to be used.

React.useEffect(() => {
  setHiddenColumns(hiddenColumns)
}, [hiddenColumns])

Well that's one way, but it looks like you're trying to fit a React lifecycle method into a React effect hook. Using the following piece of code would achieve the same in the way it's meant to be used.

React.useEffect(() => {
  setHiddenColumns(hiddenColumns)
}, [hiddenColumns])

Wow..thanks @zebrahondje , that's clean and proper way..

@zebrahondje I am using the above code in my project, but it is not working.

@zebrahondje I am using the above code in my project, but it is not working.

@meghna1711 It's working fine on my project
Maybe you are not passing the correct updated list on the updateHiddenColumns. It should be a list of id. Try to log the hidden columns state like this example

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prathmeshphuke picture prathmeshphuke  路  33Comments

nikhilem picture nikhilem  路  27Comments

Grsmto picture Grsmto  路  100Comments

JayV30 picture JayV30  路  26Comments

Oskii2311 picture Oskii2311  路  46Comments