fix: null guard toFixed in ThemeStockRow to prevent crash when API returns null

This commit is contained in:
Sakurasan
2026-08-22 00:10:48 +08:00
parent 1f9931da77
commit 57548c791d
+5 -5
View File
@@ -351,11 +351,11 @@ function NewsList({
function ThemeStockRow({ stock }: { stock: ThemeStock }) {
const [showReason, setShowReason] = useState(true); // 入选理由默认展开
const board = getStockBoard(stock.securityCode);
const isPos = stock.f3 >= 0;
const isPos = (stock.f3 ?? 0) >= 0;
const reasons = stock.keywordList ?? [];
// 换手率:接口返回放大 100 倍的值(如 3733 = 37.33%)
const turnoverRate = stock.f8 > 100 ? stock.f8 / 100 : stock.f8;
const turnoverRate = stock.f8 != null ? (stock.f8 > 100 ? stock.f8 / 100 : stock.f8) : null;
return (
<Link to="/stock/$code" params={{ code: stock.securityCode }} className="block">
@@ -381,13 +381,13 @@ function ThemeStockRow({ stock }: { stock: ThemeStock }) {
<div className="flex items-center gap-3 shrink-0">
<div className="text-right">
<p className="text-[10px] text-muted-foreground">现价</p>
<p className="text-sm font-semibold tabular-nums">{stock.f2.toFixed(2)}</p>
<p className="text-sm font-semibold tabular-nums">{stock.f2 != null ? stock.f2.toFixed(2) : "--"}</p>
</div>
<div className="text-right min-w-[56px]">
<p className="text-[10px] text-muted-foreground">涨跌</p>
<p className={`text-sm font-bold tabular-nums ${isPos ? "text-red-500" : "text-green-500"}`}>
{isPos ? "+" : ""}
{stock.f3.toFixed(2)}%
{stock.f3 != null ? stock.f3.toFixed(2) : "--"}%
</p>
</div>
</div>
@@ -398,7 +398,7 @@ function ThemeStockRow({ stock }: { stock: ThemeStock }) {
{stock.f100 && (
<span className="truncate bg-muted rounded px-1.5 py-0.5 text-[10px]">{stock.f100}</span>
)}
<span className="shrink-0 tabular-nums">换手 {turnoverRate.toFixed(2)}%</span>
<span className="shrink-0 tabular-nums">换手 {turnoverRate != null ? turnoverRate.toFixed(2) : "--"}%</span>
<span className="shrink-0 tabular-nums">主力 {formatMoney(stock.f62)}</span>
<span className="shrink-0 tabular-nums ml-auto">成交 {formatMoney(stock.f6)}</span>
</div>