简体   繁体   English

React - 函数组件在将函数作为 props 传递时保持重新渲染

[英]React - functional components keep re-render when passing functions as props

i have an issue in my react app and i dont know how to solve it;我的 React 应用程序有问题,我不知道如何解决;

i have an array with values and chosen list and a function to add item to the chosen list我有一个包含值和所选列表的数组以及一个将项目添加到所选列表的函数

import React, { useState } from "react";
import Parent from "./Parent";
export default function App() {
  const [chosenList, setChosenList] = useState([]);
  const array = ["dsadas", "dasdas", "dasdasd"];

  const addToChosenList = string => {
    setChosenList([...chosenList, string]);
  };

  return (
    <div className="App">
      <Parent
        arr={array}
        chosenList={chosenList}
        addToChosenList={addToChosenList}
      />
    </div>
  );
}

Parent component that mapping through the array and give the Nested component the props: item, addToChosenList, inList通过数组映射并为嵌套组件提供道具的父组件:item、addToChosenList、inList

import React from "react";
import Nested from "./Nested.js";

export default function Parent({ arr, addToChosenList, chosenList }) {
  return (
    <div className="App">
      {arr.map((item, index) => (
        <Nested
          key={index}
          item={item}
          addToChosenList={addToChosenList}
          inList={chosenList.findIndex(listitem => listitem === item) > -1}
        />
      ))}
    </div>
  );
}

Nested component that displays the item and giving it the addToChosenList function to add the item to the chosen list显示项目并为其提供 addToChosenList 函数以将项目添加到所选列表的嵌套组件

import React, { memo } from "react";
export default memo(function Parent({ item, addToChosenList, inList }) {
  const childFunctionToAddToChosenList = () => {
    addToChosenList(item);
  };
  return (
    <div className="App" onClick={childFunctionToAddToChosenList}>
      <div>{item}</div>
      {inList && <div>in List</div>}
    </div>
  );
});

every Nested component keeps re-render after i clicked only one item in the list i believe it renders because of the function addToChosenList that changes when i change the state在我只单击列表中的一项后,每个嵌套组件都会保持重新渲染,我相信它会渲染,因为 addToChosenList 函数会在我更改状态时发生变化

anyone knows how to solve it ??有谁知道怎么解决??

thanks :)谢谢 :)

addToChosenList will point to a new reference on every re-render, wrap it in useCallback which will keep the same reference across re-renders unless one of the variables inside of the dependencies array has changed, if we pass an empty array the function will keep the same reference across the entire component lifecycle. addToChosenList将在每次重新渲染时指向一个新的引用,将它包装在useCallback 中,这将在重新渲染时保持相同的引用,除非依赖项数组中的一个变量发生了变化,如果我们传递一个空数组,该函数将保留在整个组件生命周期中使用相同的引用。

you will also need to use a functional update to avoid stale state due to the closure您还需要使用功能更新来避免由于关闭而导致的陈旧状态

const addToChosenList = useCallback(string => {
  setChosenList(prevState => [...prevState, string]);
}, []);

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

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