[英]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.useState将sortClicked
保存为一种状态,并在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文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.