[Bug]: Toaster throws in development when it has a style prop
维护者通常 2 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
- 领域
- frontend
调研方向
在本 monorepo 的 react-toast 包中找到 useToaster_unstable 和 useToasterStyles_unstable。useToaster_unstable 将相同的 rootProps(因而也包括调用方的 style)传给全部六个位置插槽,而 useToasterStyles_unstable 会写入 state.<slot>.style,使用的是 Object.assign。先针对本地构建运行 gist 的 repro.cjs,查看 throw 和共享的位置样式。完成是指每个插槽都获得一个携带自身位置样式的新对象,并且 repro 会打印出不同的 bottom-start 和 bottom-end 值而不抛出 throw。
由索引模型根据 Issue 内容生成。
描述
Component
Toast
Package version
9.75.2
React version
19.3.1
Environment
System:
OS: Windows 11 10.0.26200
CPU: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
Memory: 15.7 GB
Binaries:
Node: 26.13.1
npm: 12.2.3
npmPackages:
@fluentui/react-components: 9.75.2 => 9.75.2
@fluentui/react-toast: 9.8.5 => 9.8.5
react: 19.3.1 => 19.3.1
react-dom: 19.3.1 => 19.3.1
Current Behavior
A Toaster with a style prop throws in a development build as soon as it renders a second time with a toast.
<Toaster style={{ zIndex: 5 }} />
Dispatch one toast, then dispatch a second one or dismiss the first one:
Cannot assign to read only property 'bottom' of object '#<Object>'
The Toaster writes its position styles into the object that was passed as style. React freezes a style object in development once it has rendered it. The Toaster renders again on its own when a toast is added or dismissed, gets the same object from its props, and the write throws. A new object for every render of the parent does not help, because the parent does not render in between.
The same write also mixes up the positions. With toasts in bottom-start and bottom-end in one render, both containers get the styles of both:
bottom-start: z-index: 5; bottom: 16px; left: 20px; right: 20px;
bottom-end: z-index: 5; bottom: 16px; left: 20px; right: 20px;
So the bottom-end toasts are placed at the left edge, on top of the bottom-start ones.
The cause, on master:
useToaster_unstablecreates the six position slots from the samerootProps, so every slot holds the caller'sstyleobject.useToasterStyles_unstablerunsstate.bottomStart.style ??= {}andObject.assign(state.bottomStart.style, getPositionStyles(...))for each slot. With astyleprop the??=keeps the caller's object, andObject.assignwrites into it.
Without a style prop every slot gets its own new object, so the default Toaster is not affected. Passing the styles through className avoids both problems.
Output of the script in the reproduction link:
CASE 1 one toast, then a second toast
threw: Cannot assign to read only property 'bottom' of object '#<Object>'
CASE 2 one toast, then dismiss it
threw: Cannot assign to read only property 'bottom' of object '#<Object>'
CASE 3 toasts in bottom-start and bottom-end in one render
rendered:
bottom-start: z-index: 5; bottom: 16px; left: 20px; right: 20px;
bottom-end: z-index: 5; bottom: 16px; left: 20px; right: 20px;
I see the same in a browser on master (f4010328), in a Cypress component test with Electron 130 and React 19.2.0. With the style prop, a second toast or a dismissal throws and React unmounts the tree. Without the style prop the same steps work.
Expected Behavior
The Toaster does not write into the object passed as style, and each position container gets its own styles: the caller's styles with the styles of its own position on top.
Building a new object for each slot does that:
state.bottomStart.style = {
...state.bottomStart.style,
...getPositionStyles(TOAST_POSITIONS.bottomStart, state.dir, state.offset),
};
Reproduction
https://gist.github.com/jakesalvesen/412d93debd198e97633503c08f170747
Steps to reproduce
- In an empty directory run
npm i @fluentui/[email protected] react@19 react-dom@19 jsdom. - Save
repro.cjsfrom the gist and runnode repro.cjs. App.jsxshows the same cases in a browser, in a development build.
The script renders <Toaster style={{ zIndex: 5 }} />, dispatches toasts through useToastController and prints the error or the inline styles of the position containers.
Are you reporting an Accessibility issue?
no
Suggested severity
Medium - Has workaround
Products/sites affected
No response
Are you willing to submit a PR to fix?
yes
Validations
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- The provided reproduction is a minimal reproducible example of the bug.
- 主要语言
- TypeScript
- 星标
- 20.4k
- 派生
- 3k
- 平均合并
- 3 天 15 小时
- 30 天内合并 PR
- 74
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/fluentui 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 85/100
维护者通常 2 天内回复
-
[Bug]: AriaLiveAnnouncer holds back a message announced in the commit that mounts it可能已有人在做 @dmytrokirpa 于 2 天前认领。 未关闭Area: Accessibility Fluent UI react-components (v9) Type: Bug :bug:
难度 2/5 1-3 小时 新手友好度 85/100
microsoft/fluentui#37202 · 1 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
[Bug]: MessageBar announces the title and body without a space between them可能已有人在做 @jakesalvesen 于 2 天前认领。 未关闭Fluent UI react-components (v9) Type: Bug :bug:
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 2 天内回复
-
[Bug]: fluent-radio-group ignores value changes while disabled可能已有人在做 @chrisdholt 于 1 天前认领。 未关闭Fluent UI WC (v3) web-components
难度 2/5 1-3 小时 新手友好度 72/100
microsoft/fluentui#37198 · 已指派 1 人 ·
维护者通常 2 天内回复
-
[Bug]: Accordion crashes for scalar `defaultOpenItems={0}` (headless and styled v9)可能已有人在做 @arniebradfo 于 22 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
查看 microsoft/fluentui 的全部 Issue
相似的 Issue
-
generate_image reference URL guard lets through IPv4-mapped IPv6 hosts and 100.64.0.0/10可能已有人在做 @goranai 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
Inbox permission: an allow sent with `updatedInput` is ignored by Claude Code on its own safety checks可能已有人在做 @Raj1v 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
backnotprop/plannotator#4046 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
traPtitech/knoQ_R-UI#290 ·
维护者通常 24 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 64/100
farion1231/cc-switch#9981 · 2 条评论 ·
维护者通常 1 天内回复
-
account bug course exam iris lecture
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复