【问题标题】:React Spring and Styled ComponentsReact Spring 和样式化组件
【发布时间】:2020-03-08 10:56:22
【问题描述】:

我是 React 新手,正在尝试集成一些动画。我正在使用 Gatsby.js 框架。

const LeadHeading = styled(animated.h1)`
  font-weight: bold;
  font-size: 3rem;
  font-family: ${props => props.theme.fontSecondary};
  margin-bottom: 0;
  line-height: 2.5rem;  
  color: #FFF3D8;

const IndexPage = ({menuOpen}) => {

  const h1Animation = useSpring({
    opacity: menuOpen ? '0' : '1'
  })

  return (
    <>
      <Layout>
        <Section className="hero is-fullheight">
          <div className="hero-body container is-flex">
            <LeadHeading 
              style={h1Animation}
            >
              some heading
            </LeadHeading>           
          </div>
        </Section> 
      </Layout>   
    </>
  )
}
export default IndexPage

菜单状态通过 useState 钩子在“布局”组件中进行管理。

const [menuOpen, setMenuOpen] = useState(false)

基本上,我只是想在菜单弹出时淡出 h1,因为由于菜单的透明度,它看起来不太好。

感谢您的帮助。

【问题讨论】:

  • 没有“道具”,你解构了“menuOpen”。
  • 当然是的,我已经重写了一些代码..仍然无法正常工作。抱歉,我无法编辑问题

标签: reactjs gatsby react-spring


【解决方案1】:
const LeadHeading = styled(animated.h1)`
  font-weight: bold;
  font-size: 3rem;
  font-family: ${props => props.theme.fontSecondary};
  margin-bottom: 0;
  line-height: 2.5rem;  
  color: #FFF3D8;
`


const IndexPage = ({menuOpen}) => {

  const h1Animation = useSpring({
    opacity: menuOpen ? '0' : '1'
  })


  return (
    <>
      <Layout>
        <Section className="hero is-fullheight">
          <div className="hero-body container is-flex">
            <LeadHeading 
              style={h1Animation}
            >
              some heading....
            </LeadHeading>           
          </div>
        </Section> 
      </Layout>   
    </>
  )
}
export default IndexPage

EDIT//菜单状态通过useState钩子在“布局”组件中进行管理。 但是应该可以将数据传递给“IndexPage”吧?

好吧,我想我可以跳过渲染方法中的动画部分,因为我在上面的样式化组件变量中使用了它。 (对不起,我忘了在我之前的帖子中提到这一点)。 您的方法是我最初的方法之一,但没有奏效。

【讨论】:

  • 它也应该以这种方式工作。但它是否可以在没有使用普通动画.h1 样式的情况下工作?如果这不起作用,则问题出在您的代码或配置中的其他地方。
  • 我正在使用 gatsby 框架。有一个名为“gatsby-layout-plugin”的插件,它支持在“布局”和“页面”之间传递数据。 gatsbyjs.org/packages/gatsby-plugin-layout
猜你喜欢
  • 2019-12-30
  • 2020-09-26
  • 2020-06-03
  • 2020-05-10
  • 2019-02-18
  • 2019-01-19
  • 2021-04-13
  • 2021-09-06
  • 2020-11-05
相关资源
最近更新 更多