【发布时间】:2023-04-02 16:29:01
【问题描述】:
我想在 React 中为一些文本设置动画,基于商店中值的更改淡出和淡入文本对象。我在想像 useEffect 这样依赖于所述商店的东西可以解决这个问题,但我不知道如何去实现它。
我想要动画的值是下面代码中的{selection.price.toFixed(2)}。每当markets 更改时,我希望新文本在状态更改期间淡出并在短时间内突出显示:
export const MatchPrices = () => {
const markets = useStore($markets);
const updateNotificationRef = useRef();
useEffect(() => {
if (markets.length === 0) {
return;
}
updateNotificationRef.current.animate(
{
opacity: [0, 1, 0],
},
1000
);
}, [markets]);
return (
<Card className="price-display">
<Table className="card-table prices-table" size="sm">
<thead className="card-header">
<tr>
<th>Home</th>
<th>Draw</th>
<th>Away</th>
</tr>
</thead>
<tbody>
<tr>
{markets.length !== 0 ? (
markets
.filter((market) => market.mt === "MATCH_WINNER")[0]
.selections.map((selection) => (
<td
className="match-price"
key={selection.id}
ref={updateNotificationRef}
style={{ opacity: 1 }}
>
{selection.price.toFixed(2)}
</td>
))
) : (
<td>
<Spinner
animation="border"
role="status"
style={{
marginLeft: "Auto",
marginRight: "Auto",
}}
></Spinner>
</td>
)}
</tr>
</tbody>
</Table>
</Card>
);
};
我不太确定如何让 CSS 工作。
【问题讨论】:
标签: javascript css reactjs react-hooks react-bootstrap