question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to update state.hiddenColumn=[]?

See original GitHub issue

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)
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

10reactions
zebrahondjecommented, Feb 3, 2020

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])
2reactions
izzuddinraffarcommented, Feb 3, 2020

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);
    }
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update state.hiddenColumn=[]? · Issue #1880 - GitHub
Hi, I have an external function to trigger state.hiddenColumn provided in react table v7 using useEffect. The table and hidden columns is ...
Read more >
Has column show property stopped working in latest react ...
Instead of setting show in the column, set the initialState.hiddenColumns , here you can convert your existing show into the initial hidden ......
Read more >
React Table Tutorial - 16 - Column Hiding - YouTube
React Table Tutorial - 16 - Column Hiding ; Courses - https://learn.codevolution.dev/ ; Support UPI - https://support.codevolution.dev/ ;
Read more >
React Table Column Visibility Example | TanStack Table Docs
An example showing how to implement Column Visibility in React Table.
Read more >
Hidden Columns - Grid - Kendo UI for Angular - Telerik
You can configure the hidden state of the columns by: Using the built-in configuration options of the Grid. Using the Angular structural directives....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found