javascript - 样式属性中没有变量
问题描述
这很难解释,但是,事情是这样的:当我尝试进入getFadeContainerKeyFrame
时const FadeContainer = ...
,一切正常。另一方面,当我尝试从 style 中的属性中获取相同的 const 时FadeContainer
,它不会……看在上帝的份上,有人可以向我解释一下吗?蒂亚!
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import styled, { keyframes } from 'styled-components'
import styles from '../Header.css'
const getFadeContainerKeyFrame = ({ animatingOut, direction }) => {
if (!direction) return;
return keyframes`
to {
transform: translateX(0px);
opacity: ${animatingOut ? 0 : 1};
}
`
}
const FadeContainer = styled.div`
animation-name: ${getFadeContainerKeyFrame}; // <- this works ...
`
const propTypes = {
direction: PropTypes.oneOf(["right", "left"]),
animatingOut: PropTypes.bool,
children: PropTypes.node
}
const FadeContents = forwardRef(
({ children, animatingOut, direction }, ref) => (
<FadeContainer
className={styles.fadeContainer}
style={{
animationName: getFadeContainerKeyFrame, // <- this does not work ...
opacity: direction && !animatingOut ? 0 : 1,
}}
// prevent screen readers from reading out hidden content
aria-hidden={animatingOut}
animatingOut={animatingOut}
direction={direction}
ref={ref}
>
{children}
</FadeContainer>
)
)
FadeContents.displayName = 'FadeContents'
FadeContents.propTypes = propTypes
export default FadeContents;
解决方案
您确定将有效的道具发送给该getFadeContainerKeyFrame
功能吗?如果你console.log(animatingOut, direction)
在里面getFadeContainerKeyFrame
,道具是不是印出来的?
试着做:
const getFadeContainerKeyFrame = (animatingOut, direction) => {
if (!direction) return;
return keyframes`
to {
transform: translateX(0px);
opacity: ${animatingOut ? 0 : 1};
}
`
}
<FadeContainer
className={styles.fadeContainer}
style={{
animationName: getFadeContainerKeyFrame(animatingOut, direction)
opacity: direction && !animatingOut ? 0 : 1,
}}
aria-hidden={animatingOut}
animatingOut={animatingOut}
direction={direction}
ref={ref}
>
{children}
</FadeContainer>
推荐阅读
- spring - Angular - 为什么表单中的数据不会传递给 component.ts
- javascript - 试图将对象作为json文件从spring数据下载到angular 4
- reporting-services - 在 Microsoft Dynamics AX 2012 中部署 SSRS 报告时出错
- c++ - 如何在具有不同循环的行和列的向量向量中使用 find_if
- c# - MVVM Light SimpleIoc Unregister 实例失败
- cntk - CNTK 2.5.1 Function::Load 溢出未处理的异常“未找到模块”
- wordpress - 如何重新链接“登录以回复”。关联
- javascript - 触发输入键时更改按钮样式
- react-native - `react-native init app` 在 React-Native 0.56 中不起作用
- python - 将图像分割成辐条并计算每个辐条中黑色像素的数量