跳到主要内容

stylex.attrs

信息

Just like stylex.props but the return object uses class instead of className, and converts style to a string.

Takes an StyleX style or array of StyleX styles, and returns a props object. Values can also be null, undefined, or false.

The return value should be onto an element to apply the styles directly.

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

Example use:

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),
)}
/>;