使用说明
替换一个稳定位置
swapChild(key, spec) 的第二个参数不再包含 key。父级对该 key 的查询和 route 可以保持稳定,而实际 child 定义会被替换。
const receipt = await panel.swapChild('submit', { artifact: DangerButton({ label: '删除' }), parentKey: null, mount: { kind: 'root' }, props: { disabled: false }, statePolicy: 'reset',});
if (receipt.status === 'committed') { console.log(panel.child('submit')?.instanceId);}使用相同 exact ref 或同一个 Artifact object 时,statePolicy: 'preserve' 可能复用现有 Instance 并只 patch props、slots 与 layout。需要强制重新创建时使用 statePolicy: 'reset'。
await panel.swapChild('submit', { ref: nextButtonRef, parentKey: null, mount: { kind: 'root' }, props: { label: '继续' }, statePolicy: 'preserve',}, { signal: abortController.signal, timeoutMs: 15_000,});如果 transaction 没有提交,swapChild() 会恢复调用前保存的 spec。与该 key 关联的其他 parentKey、公开引用和 route 无需改名;其嵌套 children 仍按完整 desired set 参与本次 reconcile。
该方法不会修改原始 Artifact,也不会发布新的 Component Release。