简体   繁体   English

为什么回调后组件不重新渲染?

[英]Why does the component not re-render after callback?

Given the following two components, I expect the EntryList component to re-render after the state changes in the handleEnttryDelete after the button in EntryForm is clicked.鉴于以下两个组件,我希望在单击 EntryForm 中的按钮后,在 handleEnttryDelete 中的 state 更改后, EntryList 组件将重新呈现。 Currently the state changes, but the UI isn't updating itself:目前 state 发生变化,但 UI 并未自行更新:

import React, { useState } from "react";

import Button from "@material-ui/core/Button";
import { render } from "@testing-library/react";

const EntryList = (props) => {
  const [entryList, setEntryList] = useState(props.data);

  const handleEntryDelete = (entry) => {
    const newState = entryList.filter(function (el) {
      return el._id != entry._id;
    });
    setEntryList(() => newState);
  };

  return (
    <div>
      {entryList.map((entry) => {
        return (
          <EntryForm entry={entry} handleEntryDelete={handleEntryDelete} />
        );
      })}
    </div>
  );
};

const EntryForm = (props) => {
  const [entry, setEntry] = useState(props.entry);

  return (
    <div>
      <Button onClick={() => props.handleEntryDelete(entry)}>
        {entry._id}
      </Button>
    </div>
  );
};

export default EntryList;

Your code probably works, but not as intended.您的代码可能有效,但未达到预期。 You just have to use key while mapping arrays to components.您只需在将 arrays 映射到组件时使用key

Therefore, React can distinguish which elements should not be touched during reconciliation when you delete one of the nodes因此,React 可以区分当你删除其中一个节点时,在对账过程中哪些元素不应该被触动

<div>
  {entryList.map((entry) => {
    return <EntryForm key={entry._id} entry={entry} handleEntryDelete={handleEntryDelete} />;
  })}
</div>;

样式化组件不更新<div>重新渲染后</div><div id="text_translate"><p>我有一个基于 switch 语句呈现的反应组件。 我正在更新开关基于的 state,但它没有</p><pre>switch (condition) { case Condition1: default: return ( &lt;Condition1Component /&gt; ); case Condition2: return ( &lt;Condition2Component /&gt; ); case Condition3: return ( &lt;Condition3Component /&gt; ); }</pre><p> 所有三个组件都被包装在自己的样式中div 。</p><p> 当我 go 从默认 state 到Condition3时, Condition3组件被包裹在Condition1 styled div周围,这很奇怪。</p><p> 当我将我的默认 state 更改为Condition3时,一切都按预期工作。</p></div> - Styled Component does not update the <div> after a re-render

暂无
暂无

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

相关问题 为什么在setState之后我的组件不重新渲染? - Why does not my component re-render after setState? 样式化组件不更新<div>重新渲染后</div><div id="text_translate"><p>我有一个基于 switch 语句呈现的反应组件。 我正在更新开关基于的 state,但它没有</p><pre>switch (condition) { case Condition1: default: return ( &lt;Condition1Component /&gt; ); case Condition2: return ( &lt;Condition2Component /&gt; ); case Condition3: return ( &lt;Condition3Component /&gt; ); }</pre><p> 所有三个组件都被包装在自己的样式中div 。</p><p> 当我 go 从默认 state 到Condition3时, Condition3组件被包裹在Condition1 styled div周围,这很奇怪。</p><p> 当我将我的默认 state 更改为Condition3时,一切都按预期工作。</p></div> - Styled Component does not update the <div> after a re-render 为什么此功能组件在setState之后不重新呈现? - Why this function component doesn't re-render after setState? setState之后,React不会重新渲染组件 - React does not re-render component after setState state 更新后 React 不会重新渲染组件 - React does not re-render component after state update 为什么 useQuery 调用会导致我的组件重新渲染? - Why does useQuery call cause a re-render in my component? 道具更改时,为什么我的组件会重新渲染? - Why does my component re-render when a prop changes? 子组件不会重新渲染 - Child component does not re-render 为什么 eventListener 重新渲染 React 组件 - Why eventListener re-render React Component 祖父母回调中的反应组件在孙子调用后不会重新呈现页面 - React component of grandparent callback doesnt re-render page after being call in grandchild
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM