【发布时间】:2021-12-13 18:17:22
【问题描述】:
PSA:
(不是问题,但我在堆栈上没有看到任何答案,所以这是答案。)
在包装动作的样式组件上定义道具,在使用styled() 包装动作时如何定义它们有点令人困惑。
如何根据样式化组件定义props docs
import styled from 'styled-components';
import Header from './Header';
interface TitleProps {
readonly isActive: boolean;
}
const Title = styled.h1<TitleProps>`
color: ${(props) => (props.isActive ? props.theme.colors.main : props.theme.colors.secondary)};
`;
如何在代码中不定义道具:
import styled from "styled-components";
interface Props {
height?: number;
}
const Container = styled.div<Props>`
height: ${({ height }) => height};
`;
export default Container;
如何在你的代码中定义道具with动作:
import { HTMLMotionProps, motion } from "framer-motion";
import styled from "styled-components";
/**
* notice the props extend HTMLMotionProps<"div"> this is so all the default
* props are passed such as `onClick`
*/
interface Props extends HTMLMotionProps<"div"> {
height?: number;
}
//notice motion is called as a function instead of `motion.div`
const Container = styled(motion<Props>("div"))`
height: ${({ height }) => height};
`;
export default Container;
【问题讨论】:
标签: reactjs typescript styled-components typescript-generics framer-motion