每次输入内容时都会重新渲染功能组件

问题描述 投票:0回答:1

我正在使用react-table版本7。我正在使用全局过滤器进行过滤,我从沙箱中复制了一些代码并将其集成到我的数据库中,这是我从中复制代码Open Sandbox的链接。问题是,每当我在搜索栏中写一些东西,重新渲染整个全局过滤器组件,并且每次输入字段都失去焦点时,我就不得不再次单击输入字段来输入文本,这很烦人由于重新渲染需要时间,因此我每次都必须单击输入字段。为什么会发生这种情况,为什么在反应表全局过滤器的沙箱示例中不会发生相同的事情。您可以在上面包含的链接上查看示例。

 return (
    <>
        <GlobalFilter
            preGlobalFilteredRows={preGlobalFilteredRows}
            globalFilter={state.globalFilter}
            setGlobalFilter={setGlobalFilter}
          />

        {/* //rest of the code*/}
         .....
    </> );

    function GlobalFilter({
      preGlobalFilteredRows,
      globalFilter,
      setGlobalFilter,
      }) {
    const count = preGlobalFilteredRows.length

    return (
      <span>
        Search:{' '}
        <input
          value={globalFilter || ''}
          onChange={e => {
            setGlobalFilter(e.target.value || undefined) // Set undefined to remove the filter entirely
          }}
          placeholder={`${count} records...`}
          style={{
            fontSize: '1.1rem',
            border: '0',
          }}
        />
      </span>
    )
  }

如果我直接在return语句内编写全局过滤器的代码,则不会发生此问题,使用此方法整个组件不会重新呈现。像这样。

return(
  <>
  div className="row" style={{ 'zoom': '94%' }}>
            <div className="col">
                <div class="student-search-field">
                    <h4>Search: &nbsp;<input
                        value={state.globalFilter || ""}
                        onChange={e => {
                            setGlobalFilter(e.target.value || undefined); // Set undefined to remove the filter entirely
                        }}
                        placeholder={`  ${count} Students..`}
                        style={{
                            fontSize: "1.1rem",
                            border: "1",

                        }}
                    /></h4>

                </div>


            </div>

        </div>
{/* //rest of the code*/}
</>);

我其余的代码是这样的

她是我的完整代码。

import React from 'react';
import { useLocation } from "react-router-dom";
import { useTable, usePagination, useGlobalFilter } from 'react-table';

import './classdetails.css';


function SimpleTable(props) {

const location = useLocation();
var paginationDisplay = { display: '' };
console.log(location.state);
const data = React.useMemo(
    () => [
        {
            col1: 'john doe',
            col2: "world",

        },
        {
            col1: 'react-table',
            col2: 'rocks',
        },
        {
            col1: 'whatever',
            col2: 'you want',
        },
        {
            col1: 'whatever',
            col2: 'you want',
        },
        {
            col1: 'whatever',
            col2: 'you want',

        },
        {
            col1: 'whatever',
            col2: 'you want',

        },
        {
            col1: 'whatever',
            col2: 'you want',

        },
        {
            col1: 'whatever',
            col2: 'you want',

        },
        {
            col1: 'whatever',
            col2: 'you want',

        },

    ],
    [],
);
const columns = React.useMemo(
    () => [
        {
            Header: 'STUDENT NAME',
            accessor: 'col1', // accessor is the "key" in the data
            Cell: (row) => {
                return (
                    <>
                        {row.row.original.col1}
                        <p>FA16-BCS-067</p>
                    </>);
            },

        },
        {
            Header: 'ATTENDANCE',
            accessor: 'col2',// use accessor name in hidden column to hide a column e.g intitalstate.hiddenColumns['col2']
        },
        {
            Header: 'QUIZEZ',
            accessor: '',

        },
        {
            Header: 'ASSIGNMENT',
            accessor: '',
        },
        {
            Header: 'FIRST TERM',
            accessor: '',
        },
        {
            Header: 'MID TERM',
            accessor: '',

        },
        {
            Header: 'FINAL TERM',
            accessor: '',

        },
        {
            Header: 'action',
            accessor: '',
            Cell: (row) => {
                return (
                    <button className="btn btn-danger"
                        onClick={() => console.log(row.row)}
                    >
                        View
                    </button>);
            },

        },
    ],
    [],
);


  function GlobalFilter({
    preGlobalFilteredRows,
    globalFilter,
    setGlobalFilter,
  }) {
    const count = preGlobalFilteredRows.length

    return (
      <span>
        Search:{' '}
        <input
          value={globalFilter || ''}
          onChange={e => {
            setGlobalFilter(e.target.value || undefined) // Set undefined to remove the filter entirely
          }}
          placeholder={`${count} records...`}
          style={{
            fontSize: '1.1rem',
            border: '0',
          }}
        />
      </span>
    )
  }



const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    page,
    prepareRow,
    canPreviousPage,
    canNextPage,
    nextPage,
    previousPage,
    setPageSize,
    pageOptions,
    state,
    preGlobalFilteredRows,
    setGlobalFilter,
    state: { pageIndex, pageSize }

} = useTable({ columns, data, initialState: { pageIndex: 0, pageSize: 5, hiddenColumns: [''] } },
    useGlobalFilter, usePagination);



const count = preGlobalFilteredRows.length;
return (
    <>

        <GlobalFilter
            preGlobalFilteredRows={preGlobalFilteredRows}
            globalFilter={state.globalFilter}
            setGlobalFilter={setGlobalFilter}
          />


        {/* //table section */}

        <div className="row">
            <div className="col table-div-1 highlight table-2" style={{ 'overflowY': 'auto', 'height': '455px' }}>
                <table {...getTableProps()}>
                    <thead>
                        {headerGroups.map(headerGroup => (
                            <tr {...headerGroup.getHeaderGroupProps()}>
                                {headerGroup.headers.map(column => (
                                    <th
                                        {...column.getHeaderProps()}
                                    >
                                        {column.render('Header')}
                                    </th>
                                ))}
                            </tr>
                        ))}
                    </thead>
                    <tbody {...getTableBodyProps()}>
                        {page.map(row => {
                            prepareRow(row)
                            return (
                                <tr key={123} {...row.getRowProps()} >
                                    {row.cells.map(cell => {
                                        return (
                                            <td
                                                {...cell.getCellProps()}
                                                onClick={() => console.log()}
                                            >
                                                {cell.render('Cell')}
                                            </td>
                                        )
                                    })}
                                </tr>
                            )
                        })}
                    </tbody>
                </table>
            </div>
        </div>

        {/* //pagination section */}

    { props === props ? <>
        <div className="row pagination" style={paginationDisplay}>
            <span>
                Page{' '}
                <strong>
                    {pageIndex + 1} of {pageOptions.length}
                </strong>{' '}
            </span>
            <button className="btn btn-danger" onClick={() => previousPage()} disabled={!canPreviousPage}>
                {'<'}
            </button>{" "}
            <button className="btn btn-danger" onClick={() => nextPage()} disabled={!canNextPage}>
                {'>'}
            </button>{" "}
            <select className="btn btn-danger"
                value={pageSize}
                onChange={e => {
                    setPageSize(Number(e.target.value));
                    console.log(pageSize);
                }}
            >
                {[5, 10, 20, 30].map(pageSize => (
                    <option key={pageSize.value} value={pageSize}>
                        Show {pageSize}
                    </option>
                ))}
            </select>
        </div>
        </> : null }
    </>

);
   }

  export default SimpleTable;
reactjs jsx react-table
1个回答
0
投票

您好,请在您的组件中使用记事,例如

export default React.memo(SimpleTable);
© www.soinside.com 2019 - 2024. All rights reserved.