跳至主要内容

stylex.attrs

信息

就像 stylex.props 一样,但是返回的对象使用 class 而不是 className,并将 style 转换为字符串。

接受一个 StyleX 样式或 StyleX 样式数组,并返回一个 props 对象。值也可以是 nullundefinedfalse

返回值应该添加到元素上以直接应用样式。

function attrs(styles: StyleXStyles | StyleXStyles[]): {
class?: string;
style?: string;
};

示例用法:

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
root: {
backgroundColor: 'red',
padding: '1rem',
paddingInlineStart: '2rem',
},
conditional: {
backgroundColor: 'blue',
},
dynamic: (opacity) => ({
opacity,
}),
});

<div
{...stylex.attrs(
styles.root,
condition && styles.conditional,
props.style,
styles.dynamic(state.opacity),
)}
/>;