代码之家  ›  专栏  ›  技术社区  ›  VikR

不变性助手更新一个属性而不是其他属性?

  •  0
  • VikR  · 技术社区  · 6 年前

    我有一个jsx风格,看起来像这样:

    let styles = {
        appBarStyle: {
            display: 'flex',
            flexWrap: 'nowrap',
            WebkitFlexFlow: 'nowrap',
            flex: '1 1 0%',
            alignItems: 'center',
            justifyContent: 'space-between',
            padding: '0',
            margin: '0',
            listStyle: 'none',
            backgroundColor: '#00bcd4',
            fontFamily: 'Roboto',
            minHeight: '64px',
            color: 'white',
            fontSize: '2em',
            margin: '0px',
            padding: '0px',
            width: '100%',
            zIndex: '2',
            position: 'static',
            top: 'auto',
            left: 'auto',
            right: 'auto'
        }
    };
    

    所以我有一些使用不变性助手的代码:

    if (useFixedMenuBar) {
        styles = update(styles, {
            appBarStyle:{top: {$set: '0px'}},
            appBarStyle:{left: {$set: '0px'}},
            appBarStyle:{right: {$set: '0px'}},
            appBarStyle:{position: {$set: 'fixed'}},
    }
    

    update ,和 position fixed ,但是 top , left ,和 right 0px --他们仍然准备 auto

    我错过了什么?

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Steve Archer    6 年前

    我认为您正在覆盖设置为appBarStyle键的内容,因此只设置了最后一个键,然后会进行更新。尝试:

    styles = update(styles, {
        appBarStyle:{
            top: {$set: '0px'},
            left: {$set: '0px'},
            right: {$set: '0px'},
            position: {$set: 'fixed'},
        }
    })