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.

useFlexLayout plugin hook error

See original GitHub issue

V7 I’m not sure if I am doing something wrong with the configuration of the useFlexLayout plugin but using it gives the following error: Cannot read property 'columns' of undefined

I am using the plugin as shown below.

  const { getTableProps, headerGroups, rows, prepareRow } = useTable({
    columns,
    data,
  },
  useFlexLayout
  )

This error occurs in version 7.0.0-alpha.13 and above

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
paolostylecommented, Aug 5, 2019

If you urgently need it here’s my updated version, works pretty much the same as the old one and is compatible with alpha.19. Since @tannerlinsley mentioned potential deprecation I guess it can be useful in the future.

import PropTypes from 'prop-types';

function sum(arr) {
  return arr.reduce((prev, curr) => prev + curr, 0);
}

function getFirstDefined(...args) {
  for (let i = 0; i < args.length; i += 1) {
    if (typeof args[i] !== 'undefined') {
      return args[i];
    }
  }
  return undefined;
}

function getSizesForColumn({ columns, id, width, minWidth, maxWidth }, defaultFlex) {
  if (columns) {
    columns = columns
      .filter(col => col.show || col.visible)
      .map(column => getSizesForColumn(column, defaultFlex))
      .filter(Boolean);

    if (!columns.length) {
      return false;
    }

    const flex = sum(columns.map(col => col.flex));
    const width = sum(columns.map(col => col.width));
    const maxWidth = sum(columns.map(col => col.maxWidth));

    return {
      flex,
      width,
      maxWidth
    };
  }

  return {
    flex: width ? 0 : defaultFlex,
    width: width === 'auto' ? defaultFlex : getFirstDefined(width, minWidth, defaultFlex),
    maxWidth
  };
}

function getStylesForColumn(column, defaultFlex) {
  const { flex, width, maxWidth } = getSizesForColumn(column, defaultFlex);

  return {
    flex: `${flex} 0 auto`,
    width: `${width}px`,
    maxWidth: `${maxWidth}px`
  };
}

const propTypes = {
  defaultFlex: PropTypes.number
};

const useMain = props => {
  PropTypes.checkPropTypes(propTypes, props, 'property', 'useFlexLayout');

  const {
    defaultFlex = 1,
    columns,
    hooks: { getRowProps, getHeaderGroupProps, getHeaderProps, getCellProps }
  } = props;

  const visibleColumns = columns.filter(column => column.visible);

  let sumWidth = 0;
  visibleColumns.forEach(column => {
    const { width, minWidth } = getSizesForColumn(column, defaultFlex);
    if (width) {
      sumWidth += width;
    } else if (minWidth) {
      sumWidth += minWidth;
    } else {
      sumWidth += defaultFlex;
    }
  });

  const rowStyles = {
    style: {
      display: 'flex',
      minWidth: `${sumWidth}px`
    }
  };

  getRowProps.push(() => rowStyles);
  getHeaderGroupProps.push(() => rowStyles);

  getHeaderProps.push(column => ({
    style: {
      boxSizing: 'border-box',
      ...getStylesForColumn(column, defaultFlex)
    }
  }));

  getCellProps.push(cell => {
    return {
      style: {
        ...getStylesForColumn(cell.column, defaultFlex)
      }
    };
  });

  return props;
};

const useFlexLayout = hooks => {
  hooks.useMain.push(useMain);
};

export default useFlexLayout;

Edit: removed some useless function parameters and general cleanup - when I was updating this hook a couple of days ago I only tried to make it work to be honest so I didn’t read too much into the code.

1reaction
tannerlinsleycommented, Aug 19, 2019

Yes. That’s a good idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFlexLayout plugin hook error · Issue #1428 · TanStack/table
I'm not sure if I am doing something wrong with the configuration of the useFlexLayout plugin but using it gives the following error:...
Read more >
Developers - useFlexLayout plugin hook error - - Bountysource
V7 I'm not sure if I am doing something wrong with the configuration of the useFlexLayout plugin but using it gives the following...
Read more >
API Reference: useFlexLayout - React Table - TanStack
useFlexLayout is a plugin hook that adds support for headers and cells to be rendered as inline-block div s (or other non-table elements)...
Read more >
Migrating React table from v6 to v7 | by Sagar Shrestha
useFlexLayout is a plugin hook that adds support for headers and cells to be rendered as inline-block div s (or other non-table elements) ......
Read more >
react-table | Yarn - Package Manager
Fixed an issue where plugins using the columns hook were not getting decorated properly; Added back a new rendition of the useFlexLayout plugin...
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