当我单击将数组的第一个元素发送到最后一个位置的按钮时,我尝试重新渲染列表,但是,当我单击该按钮时,组件不会重新渲染,即使 console.log 显示该数组已更改: codesandbox
import React, { useState } from "react";
const DailySchedule = () => {
const [exerciseList, setExerciseList] = useState([
"exercise 1",
"exercise 2",
"exercise 3"
]);
return (
<div>
<section>
<h2>Warm-up</h2>
<ul>
{exerciseList.map((exrcs, idx) => {
return (
<li>
{exrcs}{" "}
{idx === 0 && (
<button
onClick={() => {
exerciseList.push(exerciseList.shift());
setExerciseList(exerciseList);
console.log(exerciseList);
}}
>
Done
</button>
)}
</li>
);
})}
</ul>
</section>
</div>
);
};
export default DailySchedule;
慕侠2389804
动漫人物
慕丝7291255
慕勒3428872
相关分类