来自 react-transition-group 的 CSSTransition 未应用 类

CSSTransition from react-transition-group not applying classes

我正在尝试将 CSSTransition 集成到我的 Gatsby 网站,但它没有应用任何 classes。我正在使用 CSS 模块,并且我有一个 <div> 用作淡入和淡出的父级,本质上是将淡入淡出效果应用于此并在内容更改时覆盖内容。它有 class fadEffect。这是我的 app-layout 组件,以及 SASS.

AppLayout.tsx

import React, { ReactNode, useState } from 'react';

import { ApiContext } from 'contexts/ApiContext';
import { graphql, StaticQuery } from 'gatsby';
import { TransitionGroup, CSSTransition } from 'react-transition-group';

import { Devtools } from '../devtools/Devtools';
import { Footer } from '../footer/Footer';
import { Header } from '../header/Header';
import s from './AppLayout.scss';

interface AppLayoutProps {
  children: ReactNode;
  location: string;
}

const isDev = process.env.NODE_ENV === 'development';

// tslint:disable no-default-export
export default ({ children, location }: AppLayoutProps) => {
  const [fadeEffectVisible, setFadeEffectVisible] = useState(false);

  const handleFadeEffectEntered = () => {
    setTimeout(() => {
      setFadeEffectVisible(false);
    }, 50);
  };

  return (
    <StaticQuery
      query={`${NavQuery}`}
      render={(data) => (
        <>
          <ApiContext>
            <Header navigationContent={data.prismic.allNavigations.edges[0].node} />

            <CSSTransition
              in={fadeEffectVisible}
              timeout={150}
              classNames={{
                enter: s.fadeEffectEnter,
                enterActive: s.fadeEffectEnterActive,
                enterDone: s.fadeEffectEnterDone,
                exit: s.fadeEffectExit,
                exitActive: s.fadeEffectExitActive,
              }}
              onEntered={handleFadeEffectEntered}
            >
              <div className={s.fadeEffect} aria-hidden="true" />
            </CSSTransition>

            <TransitionGroup component={null}>
              <CSSTransition
                key={location}
                timeout={150}
                classNames={{
                  enter: s.pageEnter,
                }}
              >
                <div className={s.layout}>
                  {children}

                  <Footer navigationItems={data.prismic.allNavigations.edges[0].node} />

                  {isDev && <Devtools />}
                </div>
              </CSSTransition>
            </TransitionGroup>
          </ApiContext>
        </>
      )}
    />
  );
};

const NavQuery = graphql`
  query NavQuery {
    prismic {
      allNavigations {
        edges {
          node {
            ...NotificationBar
            ...NavigationItems
            ...FooterNavigationItems
          }
        }
      }
    }
  }
`;

AppLayout.scss

@import '~styles/config';

:global {
  @import '~styles/base';
}

.layout {
  display: block;
  min-height: 100vh;
}

.pageEnter {
  display: none;
}

.fadeEffect {
  display: none;
  position: fixed;
  z-index: 9;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  transition: opacity 0.15s linear;

  &Enter {
    display: block;
    opacity: 0;
  }

  &Active,
  &Done,
  &Exit {
    display: block;
    opacity: 1;
  }

  &ExitActive {
    opacity: 0;
  }
}

如果这还不够,我很乐意提供更多 details/code。我是 React 和 Gatsby 的新手,所以我仍在学习行话。提前致谢。

我没有看到您将 fadeEffectVisible 首先更新为 true 的部分代码 CSSTransition 而且我根本没有看到 in 属性在第二 CSSTransition 上,我敢打赌这是你的问题。请查看 React Transition Group 中的示例以了解属性的用法。

App.js

function App() {
  const [inProp, setInProp] = useState(false);
  return (
    <div>
      <CSSTransition in={inProp} timeout={200} classNames="my-node">
        <div>
          {"I'll receive my-node-* classes"}
        </div>
      </CSSTransition>
      <button type="button" onClick={() => setInProp(true)}>
        Click to Enter
      </button>
    </div>
  );
}

Style.css

.my-node-enter {
  opacity: 0;
}
.my-node-enter-active {
  opacity: 1;
  transition: opacity 200ms;
}
.my-node-exit {
  opacity: 1;
}
.my-node-exit-active {
  opacity: 0;
  transition: opacity 200ms;
}

in属性设置为true时,子组件会先接收到classexample-enter,然后在下一个tick中添加example-enter-active .