2017-09-13 35 views
0

我试图设置onPress来调用action来分派图标,具体取决于Redux中的状态。根据状态设置图标onPress

<MaterialIcons 
     name="arrow-upward" size={30} style={mystyles1.myIcon} 
     onPress = this.props.style.name === 'styleNormal' ? 
      {() => this.props.changeStyleNew('styleNew')} : 
      {()=>this.props.changeStyleNormal('styleNormal')} 
    > 
    </MaterialIcons> 

因此,如果this.props.style.name === 'styleNormal'第一功能(changeStyleNew)获得通过,如果不是第二个(changeStyleNormal)。我无法做到这一点。我收到错误:

TransformError C:/.../ExtComp02.js: 
JSX value should be either an expression or a quoted JSX text (94:19) 

我该如何做到这一点?非常感谢。

回答

1

你需要保持表达的大括号内{}这样

<MaterialIcons 
     name="arrow-upward" size={30} style={mystyles1.myIcon} 
     onPress = { 
      this.props.style.name === 'styleNormal' ? 
     () => this.props.changeStyleNew('styleNew') : 
     ()=>this.props.changeStyleNormal('styleNormal') 
     } 
    > 
</MaterialIcons> 
1

你可以只写一个3功能,如果语句中使用。

<MaterialIcons 
    name="arrow-upward" size={30} style={mystyles1.myIcon} 
    onPress={() => { 
    if (this.props.style.name === 'styleNormal') this.props.changeStyleNew('styleNew'); 
    else this.props.changeStyleNormal('styleNormal'); 
    }} 
> 
</MaterialIcons>