简体   繁体   English

React:防止变量重新渲染

[英]React: prevent variables from re-rendering

i recently found the issue of my problem but it's not solved yet, so i have a variable named data that have a bunch array of objects, i want to move it to global variable so it's not going to re-rendering but i can't because the fetchedData state prevent me from doing this and i can't move my state to global variable,i've tried several method like making a function outside it's component or using react.memo but it solve nothing.我最近发现了我的问题,但还没有解决,所以我有一个名为 data 的变量,它有一堆对象,我想将它移动到全局变量,这样它就不会重新渲染,但我不能因为fetchedData状态阻止我这样做并且我无法将我的状态移动到全局变量,我尝试了几种方法,比如在它的组件之外创建一个函数或使用 react.memo 但它没有解决任何问题。 is there a way to achieve this?有没有办法实现这一目标?

here's my code Table.js :这是我的代码Table.js

import DataTable from "react-data-table-component";
import Columns from "./Columns.js";
...

const Table = () => {
  let [page, setPage] = useState(1);
  const [fetchedData, setFetchedData] = useState([]);
  var [selectedRowsData, setSelectedRowsData] = useState();

  const classes = useStyles();
  const dispatch = useDispatch();

  const checkBoxProps = {
    color: "primary",
  };

  useEffect(() => {
    const fetchAPI = async () => {
      setFetchedData(
        await dispatch(
          fetchTableList(
            localStorage.getItem("userId"),
            localStorage.getItem("auth"),
            page
          )
        )
      );
    };
    fetchAPI();
  }, [page]);

  const data = [
    ...fetchedData.map((val, i) => ({
      no: val.ROW_NUMBER,
      customerId: val.CUSTOMER_ID,
      customerName: val.CUSTOMER_NAME,
      poDate: val.PO_DATE,
      branchId: val.BRANCH_ID,
      passangerId: val.PASSANGER_ID,
      passangerBank: val.PASSANGER_BANK_NAME,
      passangerBankBranch: val.PASSANGER_BANK_BRANCH,
      created: val.CREATEBY,
    })),
  ];

  const handleChange = (state) => {
    console.log(state.selectedRows);
  };

  return (
    <div>
      <TableHeader />
      <div>
        <div>
          <DataTable
            columns={Columns}
            data={data}
            selectableRows
            selectableRowsComponent={Checkbox}
            selectableRowsComponentProps={checkBoxProps}
            onSelectedRowsChange={handleChange}
            noDataComponent={"Fetching data, please wait..."}
            selectableRowsHighlight
            customStyles={CustomTableStyle}
            conditionalRowStyles={CustomRowBackColor}
            dense
          />
          <div className={classes.footer}>
            <Footer />
            <div className={classes.paginationBox}>
              <Pagination
                nextPage={nextPage}
                previousPage={previousPage}
                page={page}
                setPage={setPage}
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Table;```

useMemo can be used to control when the value of a given variable inside a functional component should be recomputed, eg useMemo可用于控制何时应重新计算功能组件内给定变量的值,例如

const data = useMemo(() => {
  return [
    ...fetchedData.map((val, i) => ({
      no: val.ROW_NUMBER,
      customerId: val.CUSTOMER_ID,
      customerName: val.CUSTOMER_NAME,
      poDate: val.PO_DATE,
      branchId: val.BRANCH_ID,
      passangerId: val.PASSANGER_ID,
      passangerBank: val.PASSANGER_BANK_NAME,
      passangerBankBranch: val.PASSANGER_BANK_BRANCH,
      created: val.CREATEBY,
    })),
  ];
}, [fetchedData])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM