如何将对象值作为props的数组传递?

huangapple go评论95阅读模式
英文:

How to pass object values in as an array of props?

问题

I have a react button component that pulls in button classnames from a module.scss file.
It works just fine if I want to select a single style/class from my object, but I can't figure out how to display multiple (in the case I want to use augment classes). I have tried fiddling around with arrays but can't seem to get it.

Button.jsx component code:

import React from "react";
import styles from "./button.module.scss";
import Link from "next/link";

const {
  buttonPrimary,
  buttonSecondary,
  buttonTertiary,
  smallButton
} = styles;

export const Button = ({
  variant = "primary",
  label,
  href,
  ...props // allows for other custom props
}) => {
  const variants = {
    primary: buttonPrimary,
    secondary: buttonSecondary,
    tertiary: buttonTertiary,
    small: smallButton
  };

  return (
    <Link href={href ? href : "#"}>
      <a className={variants[variant]}>
        {label}
      </a>
    </Link>
  );
};

Component usage in jsx page:

<Button label={'Test'} variant={'primary'}/>

Desired outcome would be to have multiple variants selectable from the list, so we can combine them E.G.:

<Button label={'Test'} variant={['primary', 'small']} />

Thanks in advance 如何将对象值作为props的数组传递?

英文:

I have a react button component that pulls in button classnames from a module.scss file.
It works just fine if I want to select a single style/class from my object, but I can't figure out how to display multiple (in the case I want to use augment classes). I have tried fiddling around with arrays but can't seem to get it.

Button.jsx component code

import React from &quot;react&quot;;
import styles from &quot;./button.module.scss&quot;;
import Link from &quot;next/link&quot;;

const {
  buttonPrimary,
  buttonSecondary,
  buttonTertiary,
  smallButton
} = styles;

export const Button = ({
                         variant = &quot;primary&quot;,
                         label,
                         href,
                         ...props // allows for other custom props
                       }) =&gt; {
  const variants = {
    primary: buttonPrimary,
    secondary: buttonSecondary,
    tertiary: buttonTertiary,
    small: smallButton
  };

    return (
      &lt;Link href={href ? href : &quot;#&quot;}&gt;
        &lt;a className={${variants[variant]}}&gt;
          {label}
        &lt;/a&gt;
      &lt;/Link&gt;
    );
};

Component usage in jsx page:

&lt;Button label={&#39;Test&#39;} variant={&#39;primary&#39;}/&gt;

Desired outcome would be to have multiple variants selectable from the list, so we can combine them E.G.:

&lt;Button label={&#39;Test&#39;} variant={[&#39;primary&#39;, &#39;small&#39;]} /&gt;

Thanks in advance 如何将对象值作为props的数组传递?

答案1

得分: 1

你需要将变体映射到scss模块中的类名。(并用空格分隔它们)

在你的组件中,它会是这样的:

import React from "react";
import styles from "./button.module.scss";
import Link from "next/link";

const {
  buttonPrimary,
  buttonSecondary,
  buttonTertiary,
  smallButton
} = styles;

export const Button = ({
  variant = "primary",
  label,
  href,
  ...props // 允许其他自定义属性
}) => {
  const variants = {
    primary: buttonPrimary,
    secondary: buttonSecondary,
    tertiary: buttonTertiary,
    small: smallButton
  };

  return (
    <Link href={href ? href : "#"}>
      <a className={variant.map(v => variants[v]).join(' ')}>{label}</a>
    </Link>
  );
};

请注意,我已经将代码中的HTML标记和JSX内容保持原样,只进行了文本的翻译。

英文:

You need to map the variants to the class names from the scss module. (And separate them with spaces)

const classNames = variant.map(v =&gt;  variants[v]).join(&#39; &#39;)

In your component it'd be:

import React from &quot;react&quot;;
import styles from &quot;./button.module.scss&quot;;
import Link from &quot;next/link&quot;;

const {
  buttonPrimary,
  buttonSecondary,
  buttonTertiary,
  smallButton
} = styles;

export const Button = ({
                         variant = &quot;primary&quot;,
                         label,
                         href,
                         ...props // allows for other custom props
                       }) =&gt; {
  const variants = {
    primary: buttonPrimary,
    secondary: buttonSecondary,
    tertiary: buttonTertiary,
    small: smallButton
  };

    return (
      &lt;Link href={href ? href : &quot;#&quot;}&gt;
        &lt;a className={variant.map(v =&gt; variants[v]).join(&#39; &#39;)}&gt;
          {label}
        &lt;/a&gt;
      &lt;/Link&gt;
    );
};

答案2

得分: 1

以下是翻译好的部分:

如果你真的想要保留在数组之外传递一个元素的可能性,你可以像这样做:

return (
  &lt;Link href={href ? href : &quot;#&quot;}&gt;
    &lt;a className={[variant].flat().map((current) =&gt; variants[current]).join(' '))}&gt;
      {label}
    &lt;/a&gt;
  &lt;/Link&gt;
);

这段代码的作用是:

  • variant 属性放入一个数组中。
  • 使用 flat 方法将可能的二维数组转换为一维数组(例如,[['primary']] 变成 [&#39;primary&#39;],一维数组保持不变)。
  • 使用 map 方法获取不同变体的数组(每个返回值替换当前元素)。
  • 将最终的数组转换为一个字符串,用空格分隔每个元素。
英文:

If you really want to keep the possibilities to pass one element outside of an array you can do something like this :

return (
  &lt;Link href={href ? href : &quot;#&quot;}&gt;
    &lt;a className={[variant].flat().map((current) =&gt; variants[current]).join(&#39; &#39;))}&gt;
      {label}
    &lt;/a&gt;
  &lt;/Link&gt;
);

What this does is :

  • Put the variant prop in a array
  • Use the flat method to transform an eventual 2D array to a 1D array (So [[&#39;primary&#39;]] become [&#39;primary&#39;] for example (a 1D array remains a 1D array)
  • Use the map method to get an array of the differents variants (each return value replace the current element)
  • Transform the final array into a string, separating each element with an empty space

huangapple
  • 本文由 发表于 2023年5月25日 17:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定