RN中 ref 的使用

本文介绍了一个名为ChatFootBar的组件,该组件用于在ChatScreenIndex中展示带有动画效果的提示信息。当触发特定事件时,ChatFootBar通过AnimatedView显示提示并自动消失。

Introduction:

    ChatFootBar为ChatScreenIndex中用到的一个组件。在ChatScreenIndex中,当监听到某一事件发生时,在ChatFootBar组件中显示一个提示的AnimatedView,若干秒后AnimatedView渐变消失。

 

ChatScreenIndex:

// 定义chatFoot
chatFoot = null;

// 调用startTipsAnimation方法
if (this.chatFoot) {
    this.chatFoot.wrappedInstance.startTipsAnimation();
    // this.chatFoot.startTipsAnimation();
}


// 引用chatFoot
<ChatFootBar
    ref={(ref) => { this.chatFoot = ref; }}
/>

 

ChatFootbar:

constructor(props) {
    super(props);
    this.startTipsAnimation = this.startTipsAnimation.bind(this);
}

// 提示动画
startTipsAnimation() {
    if (this.fadeView) {
        this.fadeView.startAnimation();
    }
}

// 渲染NewTips
renderNewTips = () => {
    return (
        <FadeInView
            ref={s => this.fadeView = s}
            style={chatFootBarStyles.fadeTipsView}
        >
            <Text style={{ fontSize: 15, color: 'red', textAlign: 'center' }}>有新推荐</Text>
        </FadeInView>
    );
};

// 页面渲染
render() {
    return (
        <View style={newProps.style}>
            {this.renderNewTips()}
        </View>
    );
}

 

class FadeInView extends Component {
    // 执行动画
    startAnimation = () => {
        this.state.fadeAnim.setValue(1);
        Animated.timing(
            this.state.fadeAnim,
            {
                toValue: 0,
                duration: 3000,
            },
        ).start();
    };
}

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值