Dynamic Opacity not changing when component rerenders in react native

后端 未结 3 1784
说谎
说谎 2021-01-04 06:31

I\'m starting to learn React Native, and for my project I created a simple Button component to reuse in my project. I set the opacity value dynamically according to the vari

3条回答
  •  清酒与你
    2021-01-04 07:31

    In my opinion correct solution is to use setOpacityTo method.

    In your render:

    render() {
      const opacityValue = this.props.disabled ? 0.5 : 1;
      return (
         { this.btn = btn; }} onPress={this.onPress}>
          {this.props.text}
        
      );
    }
    

    And next you can use setOpacityTo method in componentDidUpdate on disabled props change:

      componentDidUpdate(prevProps) {
        const { disabled } = this.props;
        if (disabled !== prevProps.disabled) {
          const opacityValue = disabled ? 0.5 : 1;
          this.btn.setOpacityTo(opacityValue);
        }
      }
    

提交回复
热议问题