stylex.attrs
資訊
就像 stylex.props
一樣,但回傳物件使用 class
而不是 className
,並且將 style
轉換為字串。
取得 StyleX 型式或 StyleX 型式陣列,並回傳 props 物件。值也可以是 null
、undefined
或 false
。
回傳值應放入元素,以直接套用型式。
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),
)}
/>;