reject:dynamic_inline_style

Dynamic Inline Styling

It is possible that we dynamically change the CSS with inline styling. As we know that the styling is just an JavaScript object, we can change it before we render it. Here is a dumb example that change style.cursor = “pointer”;.

Ideally the if condition could be related to property of the props or state.

    let style = {
        borderWidth: '1px',
        borderStyle: 'solid',
        cursor: 'default'
    };
    let bUsingCursorPointer = true;
    if (bUsingCursorPointer) {
        style.cursor = "pointer";
    }

Full code:

const ItemComponent = (prop) => {
    let style = {
        borderWidth: '1px',
        borderStyle: 'solid',
        cursor: 'default'
    };
    let bUsingCursorPointer = true;
    if (bUsingCursorPointer) {
        style.cursor = "pointer";
    }
    return (
        <div>
            <span style={style}
                  key={prop.index}
                  onClick={prop.clickHandler}>
                name: {prop.name}, price: ${prop.price}, key: {prop.index}</span>
        </div>
    );
};
  • reject/dynamic_inline_style.txt
  • Last modified: 2020/07/14 10:13
  • by chongtin