我正在try 阻止重新呈现我的子组件<FyTable />

我有名为<TableCustomer />的父组件

这是父组件,看起来像

import {
  columnDef_FilterCustomer,
  columnType_FilterCustomer,
} from "./ColumnDef";
import { useDebounce } from "@uidotdev/usehooks";

const TableCustomer = (props) => { 
  const { resetRowSelection = false } = props;

  const [filterData, setFilterData] = useState([]);     //textfield filter values
  const debounceFilter = useDebounce(filterData, 500);  //debounce, to avoid calling useQuery for every keypress
  const [tbData, setTbData] = useState({});             //useQuery result will be set

  const handleSelectedRow = useCallback((tbCustomerSeletedRow) => {
      console.log(tbCustomerSeletedRow)  //print selected row from child component
  }, [debounceFilter,columnDef_FilterCustomer, 
  tbData,columnType_FilterCustomer,resetRowSelection]);

  const handleFilter = (event) => {
       // logic...
      setFilterData(arrayValue)
  }

  // calling useQuery debounceFilter as a parameter
  const {
    isSuccess: isSuccess_get_filterCustomer,
    data: data_get_filterCustomer,
    isError: isError_get_filterCustomer,
    error: error_get_filterCustomer,
    isPending: isPending_get_filterCustomer,
    fetchStatus: fetchStatus_get_filterCustomer,

  } = query_Get_FilterCustomer(debounceFilter);

  useEffect(() => {

    if (isSuccess_get_filterCustomer) {
      setTbData(data_get_filterCustomer?.data);
    }
    if (isError_get_filterCustomer) {
      console.log(error_get_filterCustomer);
    }
  }, [isSuccess_get_filterCustomer,data_get_filterCustomer,
    isError_get_filterCustomer,error_get_filterCustomer]);

  return (
  <>
     {/* Filter Fields */}
        <TextField
          variant="filled"
          label="Customer No"
          name="custNo"
          onChange={(event) => handleFilter(event)}
        />

        <TextField
          variant="filled"
          label="Customer Name"
          name="custName"
          onChange={(event) => handleFilter(event)}
        />

        <TextField
          variant="filled"
          label="Phone No"
          name="phoneNo"
          onChange={(event) => handleFilter(event)}
        />

    {/* Customer Table */}

      <FyTable
        columnDef={columnDef_FilterCustomer}
        data={tbData}
        columnDataType={columnType_FilterCustomer}
        resetRowSelection={resetRowSelection}
        handleSelectedRow={handleSelectedRow}
      />

  </>
)}

export default TableCustomer

我的子表组件导出为memo

export default React.memo(FyTable);

一切正常,没有错误,但当我在任何过滤器TextField上输入文本时,它完全滞后,每按一次键都调用子组件

我同意如果父组件对状态值(here 100 onChange on TextFields)进行任何更改,这将重新呈现子组件

为了避免重新呈现,我try 使用useCallback钩子并记住子组件.它仍然像地狱般重现

我遗漏了什么?.请给我解释一下

推荐答案

编写一个arePropsEqual函数并决定何时重新呈现该子组件:-

const ChildComponent = (props) => {
  //logic..
  return(
    <>
    </>
  )
}

function arePropsEqual(prevProps, nextProps){
  console.log('🚀 Props are equal Triggered')
  //console.log(prevProps)
  //console.log(nextProps)

  /**
   * check prevProps and nextProps(current props) are equal
   * if all prevProps and nextProps are totally equal then return 'true'. so IT WILL NOT RE-RENDER
   * if prevProps and nextProps are not equal return 'false', then component will re-render
   */
  let isSamePropsStatus = true 

  if(JSON.stringify(prevProps?.data) === JSON.stringify(nextProps?.data)) { isSamePropsStatus= true} else {isSamePropsStatus=false}
  
  // compare rest of all props, based on your priority 

  return isSamePropsStatus
}

export default React.memo(ChildComponent, arePropsEqual);

Reactjs相关问答推荐

值在返回页面时不更新,即使我已经更新了它们

Reaction-路由-DOM v6与v5

React useEffect 问题...异步函数内的变量正在获取延迟的更新值

生产部署:控制台中 Firebase 无效 api 密钥错误

面临 React 模式中点击外部条件的问题

Yup.number().integer() 不将 1.0 视为小数,如何解决这个问题?

在 useSelector() 中使用 array.map() 如何导致组件在分派操作时重新渲染?

如何在 React 中停止嵌套 Link 和 Button 组件的事件传播?

在遵循 react-navigation 的官方文档时收到警告消息

使用新数据更新时,React 不更新具有过滤功能的深层嵌套对象数组中的变量状态

为什么刷新页面时我的localStorage PET值变成空字符串?

ReactJS 动态禁用按钮

状态链接未传递到路由页面

DatePicker MUI 如何在日历左侧显示清除文本

React 中的 Slide API Mui 在开始而不是结束时触发侦听器

如何通过 Material ui select 使下拉菜单下拉

.filter() 函数在删除函数中创建循环 - React

为什么 React 会多次调用我的应用程序的某些函数?

为什么 state redux-toolkit 是代理?

在 redux 状态更改时更新react 按钮禁用状态