javascript - 如何通过带有样式组件的 props 传递样式,而无需在组件内部创建组件

标签 javascript reactjs styled-components

如果不在 Text 组件中创建 styled-component,我找不到另一种方法来为 Prop 提供动态样式。为了提高性能,我想在 Text 组件之外创建组件。

样式化组件

import React, { ReactChildren } from 'react'
import styled from 'styled-components'


export const Text = ({ as = 'p', children, styles = {} }) => {
  const newStyles = Object.entries(styles).reduce((acc, [key, value]) => {
    return `${acc}${key}: ${value};`
  }, '')
  const Component = styled.p`
    ${newStyles}
    color: #000;
  `
  return <Component as={as}>{children}</Component>
}

期望的用法

<Text styles={{ height: "10px" }} />

输出HTML

<p styles="height: 10px" />

enter image description here

最佳答案

上面的代码令人困惑,因为您想要将样式应用到 DOM style 属性,但您还将它们作为 CSS 样式应用到类名。无需创建此组合组件,因为 styled-components 无需组合即可处理 aschildrenstyle 属性:

Edit Styled Components - "Styles" Typescript

示例 1:

import React from "react";
import styled, { Interpolation } from "styled-components";

export type TextProps = {
  as?: string | React.ComponentType<any>;
  children: React.ReactNode;
  styles?: Interpolation<React.CSSProperties>;
};

const Component = styled.p<TextProps>`
  ${({ styles }) => styles}
  color: #000;
`;

export const Text = ({
  as,
  children,
  styles
}: TextProps): React.ReactElement => (
  <Component styles={styles} as={as}>
    {children}
  </Component>
);

export default Text;

示例 2:

import styled from "styled-components";

const Text = styled.p`
  color: #000;
`;

export default Text;

示例 3:

import * as React from "react";
import styled from "styled-components";

export type TextComponentProps = {
  className?: string;
  children: React.ReactNode;
  styles?: React.CSSProperties;
};

const TextComponent = ({
  children,
  className,
  styles
}: TextComponentProps): React.ReactElement => (
  <p className={className} style={styles}>
    {children}
  </p>
);

const Text = styled(TextComponent)`
  color: #000;
`;

export default Text;

用法:

import * as React from "react";
import Box from "./Box";
import Text from "./Text";
import Text2 from "./Text2";
import Text3 from "./Text3";
import "./styles.css";

const App = (): React.ReactElement => (
  <div className="app">
    <h1>Example 1 - "styles" as CSS Styles</h1>
    <Box>
      <Text styles={{ height: "10px" }}>Hello</Text>
      <Text as="h1">Goodbye</Text>
    </Box>
    <hr />
    <h1>Example 2 - "style" as DOM styles</h1>
    <Box>
      <Text2 style={{ height: "10px" }}>Hello</Text2>
      <Text2 as="h1">Goodbye</Text2>
    </Box>
    <hr />
    <h1>Example 3 - "styles" as DOM styles</h1>
    <Box>
      <Text3 styles={{ height: "10px" }}>Hello</Text3>
      <Text3 as="h1">Goodbye</Text3>
    </Box>
  </div>
);

export default App;

输出: enter image description here

就此而言,听起来您可能正在尝试做我用 composabled-styled-components 做的事情,虽然我不推荐它,因为它 doesn't work with SSR apps .

关于javascript - 如何通过带有样式组件的 props 传递样式,而无需在组件内部创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67990513/

相关文章:

javascript - 根据图像选择更改背景图像 CSS 属性

push-notification - react native PushNotificationIOS不监听推送通知

javascript - 生产中面临 ant-design 表单验证问题

css - 目标第一个子级 css 样式组件

javascript - 仅在第一个图像上设置 jQuery attr()

Javascript 使用这样的数据结构创建对象

reactjs - 在 TypeScript 中键入 Redux 工具包的存储

javascript - 通过自定义组件将 props 添加到 styled-component

javascript - 如何从 parent 的样式组件访问 child 的 Prop ?

javascript - 如何抓取无限滚动的网站?