英文:
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
英文:
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
答案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 => variants[v]).join(' ')
In your component it'd be:
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={variant.map(v => variants[v]).join(' ')}>
{label}
</a>
</Link>
);
};
答案2
得分: 1
以下是翻译好的部分:
如果你真的想要保留在数组之外传递一个元素的可能性,你可以像这样做:
return (
<Link href={href ? href : "#"}>
<a className={[variant].flat().map((current) => variants[current]).join(' '))}>
{label}
</a>
</Link>
);
这段代码的作用是:
- 将
variant
属性放入一个数组中。 - 使用
flat
方法将可能的二维数组转换为一维数组(例如,[['primary']]
变成['primary']
,一维数组保持不变)。 - 使用
map
方法获取不同变体的数组(每个返回值替换当前元素)。 - 将最终的数组转换为一个字符串,用空格分隔每个元素。
英文:
If you really want to keep the possibilities to pass one element outside of an array you can do something like this :
return (
<Link href={href ? href : "#"}>
<a className={[variant].flat().map((current) => variants[current]).join(' '))}>
{label}
</a>
</Link>
);
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[['primary']]
become['primary']
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论