简体   繁体   English

单击排序按钮后,数据不会重新呈现

[英]data does not re-render after clicking the sort button

I have milestoneCards. 我有里程碑卡。 I want to add a sort button, that upon clicking this button the cards will be sorted by the card heading. 我要添加一个排序按钮,单击此按钮后,卡片将按卡片标题进行排序。 The sort takes place, but it does not re-render the list in the sorted order. 进行了排序,但是它没有按照排序顺序重新呈现列表。

please advise. 请指教。 thank you so much for helping me here. 非常感谢您在这里为我提供的帮助。

import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { Card, CardBody, CardTitle } from "reactstrap";

const MyMilestones = props => {
  let sortClicked = false;

  let milestoneCards =
    props.milestones.length > 0
      ? props.milestones.map(m => (
          <p key={m.id}>
            <Link to={`/milestones/${m.id}`}>{m.attributes.heading}</Link>
          </p>
        ))
      : null;

  const sortedMilestoneCards = [...props.milestones]
    .sort((a, b) => (a.attributes.heading > b.attributes.heading ? 1 : -1))
    .map(m => (
      <p key={m.id}>
        <Link to={`/milestones/${m.id}`}>{m.attributes.heading}</Link>
      </p>
    ));

  return (
    <div className="MilestoneCards">
      {
        <Card>
          <CardBody>
            <CardTitle>
              <h4>My Milestones</h4>
            </CardTitle>
            <button
              onClick={() => {
                sortClicked = true;
                console.log("before", milestoneCards);
                milestoneCards = sortedMilestoneCards;
                console.log("after", milestoneCards);
                return (milestoneCards = sortedMilestoneCards);
              }}
            >
              Sort
            </button>
            sortClicked ? ({sortedMilestoneCards}) : {milestoneCards}
          </CardBody>
        </Card>
      }
    </div>
  );
};

const mapStateToProps = state => {
  return {
    milestones: state.myMilestones
  };
};

export default connect(mapStateToProps)(MyMilestones);

It's because you need to have sortClicked to be tracked by React. 这是因为您需要通过React跟踪sortClicked

When let sortClicked = false is declared inside MyMilestones component, it's declared once on the first component mount and won't be updated when the component is re-rendered. 当在MyMilestones组件内部声明let sortClicked = false ,它在第一次组件安装时被声明一次,并且在重新渲染该组件时不会更新。

So you can save sortClicked in a state using React.useState and update it onClick . 因此,您可以使用React.useStatesortClicked保存为一种状态,并在onClick 对其进行更新。 useState is a one-off way of storing this.state value for Class Component but for one state. useState是一种一次性存储this.state值的方法,用于类组件但用于一种状态。 (I won't get into it too deep as React documentation has a thorough coverage on Introducing Hooks ) (由于React文档对Introducing Hooks进行了详尽的介绍,因此我不会对此进行深入探讨)

const MyMilestones = props => {
  // let sortClicked = false;
  // Initialize it to "false" by default.
  let [sortClicked, setSortClicked] = React.useState(false)

  let milestoneCards = ...;
  const sortedMilestoneCards = ...;

  return (
    <div className="MilestoneCards">
      {
        <Card>
          <CardBody>
            <CardTitle>
              <h4>My Milestones</h4>
            </CardTitle>
            <button
              onClick={() => {
                // Notify "React" to re-render.
                setSortClicked(true)
                // No need to return a new reference here.
              }}
            >
              Sort
            </button>
{/*              👇 Note that {} is wrapped around the whole block. */}
            {sortClicked ? sortedMilestoneCards : milestoneCards}
          </CardBody>
        </Card>
      }
    </div>
  );
};

It's because you're not updating the milestones correctly. 这是因为您没有正确更新milestones Since they're stored on Redux state, you need to add and dispatch the action that modifies the state. 由于它们存储在Redux状态下,因此您需要添加并分派修改状态的操作。

I recommend you look at the Redux documentation . 我建议您查看Redux文档

样式化组件不更新<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.

相关问题 Function 单击按钮时不重新渲染 - Function does not re-render when clicking button 点击喜欢/不喜欢按钮后如何立即重新渲染? - How can make react re-render instantly after clicking like/unlike button? 样式化组件不更新<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 Redux - 调度后应用程序不会重新渲染 - Redux - the app does not re-render after dispatching state 更新后 React 不会重新渲染组件 - React does not re-render component after state update 为什么在setState之后我的组件不重新渲染? - Why does not my component re-render after setState? getDerivedStateFromProps更新Redux Reducer状态后不重新呈现 - getDerivedStateFromProps Does not re-render after Redux Reducer state is updated setState之后,React不会重新渲染组件 - React does not re-render component after setState 为什么回调后组件不重新渲染? - Why does the component not re-render after callback? 获取单个数据后如何强制Flatlist重新渲染? - How to force Flatlist to re-render after getting a single data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM