reactjs - React Spring 和样式化组件
问题描述
我是 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,因为由于菜单的透明度,它看起来不太好。
感谢你的帮助。
解决方案
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”对吗?
好的,我想我可以跳过渲染方法中的动画部分,因为我在上面的样式化组件变量中使用了它。(对不起,我忘了在我之前的帖子中提到这一点)。您的方法是我的第一个方法之一,但没有奏效。
推荐阅读
- aws-codebuild - 无法更新 CodeBuild 中的服务角色,出现错误“该策略未附加到角色”
- java - 可选的 Spring web NullPointer 异常
- javascript - 如何在javascript中附加孩子的孩子?
- java - 如何在 AWS Lambda Java 代码中启用断言?
- c# - 如何使用 C# 代码中的产品代码或通过命令行卸载程序
- c - OpenMP 中的多个函数与 C
- javascript - 公共 LinkedIn 页面需要在 Puppeteer 中进行身份验证,但在 Chromium/Chrome 中手动粘贴 url 时不需要
- java - 来自文本文件的二维数组
- ios - 将数据从 ViewController 传递到 SwiftUI View
- html - 我的 CSS 样式表没有链接到我的 html