3

When using TextField component as a Select field with a large data set (1000 items), there is noticeable delay when mounting / unmounting the list.

import React from 'react';
import MenuItem from '@material-ui/core/MenuItem';
import TextField from '@material-ui/core/TextField';

const TextFieldSelect = () => {
  const [value, setValue] = React.useState('');

  return (
    <TextField
      select
      onChange={newValue => setValue(newValue)}
      value={value}
    >
      {listWith1000Items.map(item => (
        <MenuItem key={index} value={index}>
          `Item ${index + 1}`
        </MenuItem>
      )}
    </TextField>
  )
};

As suggested in the docs, I'm using react-window to efficiently render the large list, but as a result - I lose the functionality of being able to select the list items.

import React from 'react';
import MenuItem from '@material-ui/core/MenuItem';
import TextField from '@material-ui/core/TextField';
import { FixedSizeList } from 'react-window';

const Row = ({ index, style }) => (
  <MenuItem key={index} value={index} style={style}>
    `Item ${index + 1}`
  </MenuItem>
)

const WindowTextFieldSelect = () => {
  const [value, setValue] = React.useState('');

  return (
    <TextField
      select
      onChange={newValue => setValue(newValue)}
      value={value}
    >
      <FixedSizedList
        height={960}
        width={480}
        itemSize={48}
        itemCount={1000}
      >
        {Row}
      </FixedSizedList>
    </TextField>
  )
};

I expect that when I select an item, the value gets updated.

Robert Hung
  • 165
  • 13

1 Answers1

0

As recommended in this other stack overflow response, i started to use Autocomplete component and performance improves drastically.

Official Docs Autocomplete example: https://codesandbox.io/s/u09wym?file=/demo.js