You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HeadlessUI实现交错过渡效果遇类型错误及React子元素问题

解决HeadlessUI Transition交错加载的两类错误

一、初始错误:Type 'Promise<boolean>' is not assignable to type 'boolean | undefined'

错误原因

  1. Transition组件的show属性要求传入布尔值或undefined,但你直接传入了await checkSection(section.name)——由于checkSection是异步函数,这个表达式返回的是Promise<boolean>,类型不匹配导致类型错误。
  2. React渲染流程是同步的,不能在渲染阶段直接执行异步操作,这会打乱React的渲染机制。

解决方案

用React状态管理每个区块的显示状态,在组件挂载后通过useEffect按顺序触发延迟显示,实现交错效果:

import { useState, useEffect, Fragment } from 'react';
import { Transition } from '@headlessui/react';

export default function Card() {
  // 用对象存储每个区块的显示状态,初始都为false
  const [visibleSections, setVisibleSections] = useState<Record<string, boolean>>({});

  useEffect(() => {
    // 封装延迟函数
    const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
    
    // 按顺序逐个显示区块
    const staggerShowSections = async () => {
      for (const section of renderRoutine.sections) {
        await delay(300);
        // 更新状态,标记当前区块为可见
        setVisibleSections(prev => ({ ...prev, [section.name]: true }));
      }
    };

    staggerShowSections();
  }, []); // 空依赖数组确保只在组件挂载时执行一次

  return (
    <>
      {renderRoutine.sections.map((section) => (
        <Transition
          key={section.name}
          as={Fragment}
          appear={true}
          show={visibleSections[section.name] ?? false}
          // 示例动画类,可根据需求替换
          enter="transition-all duration-300 ease-out"
          enterFrom="opacity-0 translate-y-4"
          enterTo="opacity-100 translate-y-0"
        >
          {/* 替换为你的区块内容 */}
          <div className="p-5 border rounded-lg shadow-sm">{section.content}</div>
        </Transition>
      ))}
    </>
  );
}

二、后续错误:Error: Objects are not valid as a React child (found: [object Promise])

错误原因

你在renderRoutine.sections.map中使用了async回调函数,map执行后会返回一个Promise数组,而React只能渲染React元素数组,Promise对象不是合法的React子元素,因此抛出该错误。

解决方案

确保map的回调是同步函数,所有异步逻辑移到useEffect中处理(如上面的代码示例),让map直接返回Transition组件元素,而非Promise。

内容的提问来源于stack exchange,提问作者Furkan Öztürk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:01:03