当道具更新时,React Native组件不透明度不会更新

时间:2018-01-15 22:10:43

标签: javascript react-native jsx mobx

我有一个React Native子组件,如果disabled prop设置为true,则会将按钮呈现为半透明状态。 app最初加载后(一旦获得数据)可能会更新,因此不会是组件的初始状态。

我可以看到,一旦我与按钮交互,它就会改变它的状态,但出于某种原因,之前没有。我可以从日志和onPress行为中看到道具正在更新。我尝试了不同的方法但似乎没有解决问题。

class TestButton extends React.Component {

  constructor(props) {
    super(props);
  }

  render() {
    const buttonOpacity = (this.props.disabled  ? disabledOpacity : 1.0);
    console.log ("test disabled", this.props.disabled, buttonOpacity);

    return (
      <BubbleText style={{opacity: buttonOpacity}} onPress={
        () => ! this.props.disabled && doSomething() }>
          { this.props.testNumber }
      </BubbleText>
    );
  }
}

5 个答案:

答案 0 :(得分:7)

设置TouchableOpacity按钮的不透明度似乎存在问题。我正在使用react-native@0.55.4。如果设置了不透明度然后进行了更新,即使将新的渲染传递给组件样式,新的渲染似乎也不会更改不透明度值。

有一种使用TouchableOpacity的本地方法。如果使用disabled属性,则也可以通过禁用所有新闻事件来受益。

<TouchableOpacity
    disabled={ this.props.is_disabled }
    activeOpacity={ this.props.is_disabled ? .6 : 1 }>
    <Text>Custom Button</Text>
</TouchableOpacity>

需要注意的是,设置activeOpacity似乎并不会改变文本的不透明度,而只会改变backgroundColor。

或者使用rgba值指定不透明度确实起作用。

export class CustomButton extends Component {

    get_button_style() {
        let _button_style = [this.props.button_style]

        if (this.props.is_disabled) {
            _button_style.push({
                backgroundColor: 'rgba(0, 0, 0, .6')
            });
        }

        return _button_style;
    }

    render() {
        return(
            <TouchableOpacity
                style= { this.get_button_style() }>
                <Text> Custom Button </Text>
            </TouchableOpacity>
        )
    }
}

答案 1 :(得分:0)

很难说只是从代码片段中说,问题可能出在使用这个问题的父组件中。添加代码可能有助于确定问题所在。

抱歉没有足够的代表添加评论。

答案 2 :(得分:0)

底层组件是TouchableOpacity。外部设置不透明度似乎存在问题。在这种情况下,我通过明确定义颜色,而不是使用不透明度来解决问题:

class TestButton extends React.Component {

  constructor(props) {
    super(props);
  }

  render() {
      return (
        <BubbleText fill={this.props.disabled ? disabledFill : undefined} textStyle={this.props.disabled ? {color: disabledText} : {}} onPress={
          () => ! this.props.disabled && loadTest(this.props.navigator, this.props.set + this.props.testNumber, this.props.children)
          }>
            { this.props.testNumber }
          </BubbleText>
          );

  }
}

在我的代码的另一部分中,我添加了一个条件,将组件呈现为View,如果已禁用,则为不透明度;如果不是,则添加TouchableOpacity

答案 3 :(得分:0)

似乎是一个已知问题https://github.com/facebook/react-native/issues/17105

一种解决方法是将TouchableOpacity的内容包装在视图中,然后将不透明度样式应用于该视图,而不是直接应用于Touchable透明度。

答案 4 :(得分:0)

使用react-native-gesture-handler中的TouchableOpacity,它有一个名为containerStyle的道具,当“ this.props.is_disabled”为false或true时,您的TouchableOpacity将自动更新不透明度。没有它,您将需要重新启动应用程序以显示不透明性:

<TouchableOpacity onPress={() => {}} 
                    disabled={this.props.is_disabled} 
                    containerStyle={{
                        opacity: this.props.is_disabled ? 1 : .4,
                    }}
                    style={}>
                </TouchableOpacity>
相关问题