【发布时间】:2020-05-26 08:41:08
【问题描述】:
我在我的州显示带有地图功能的cards。
状态如下所示:
[{ position: "0", state: "pending" },
{ position: "1", state: "pending" },
{ position: "2", state: "pending" }]
在一个函数中,我将“状态”从“待处理”更改为“正确”,但它不会刷新页面上的任何内容。
export const Card: React.FC<ICardOwnProps> = ({ }) => {
const [rollingCards, setRollingCards] = useState<IRollingCard[]>([{ position: "0", state: "pending" }, { position: "1", state: "pending" }, { position: "2", state: "pending" }])
const handleClick = () => {
let newArray = rollingCards;
newArray.splice(0, 1)
setRollingCards(newArray);
}
useEffect(() => { console.log("check changes") })
return (
<StyledCard className="Card">
{rollingCards.map((v, i, a) =>
<Container key={i} className={"position" + v.position} >{v.state}</Container>
)}
<div className="card__container--button">
<button onClick={handleClick}>Oui</button>
<button>Non</button>
{rollingCards[0].state}
</div>
</StyledCard>
);
}
从现在开始,一切都没有改变,但是当我添加一个与 rollingCards 同时更新的新状态片段时,它会更新。我添加了一个计数器,然后它会更新页面。
(我也试过拼接数组看React会不会更新,没有)
知道我为什么会有这种奇怪的行为吗?
谢谢。
【问题讨论】:
标签: javascript reactjs state