延迟一段时间后,我尝试呈现一个新组件。我的方法是

if (success) {
    return
    <span className="small_font white_color border padding_5px horiz_center"
        style={{ marginTop: '-5px' }}>Amount changed</span>
    setTimeout(<Retransfer reqUserId={reqUserId} changeAccountLevel={changeAccountLevel} />, 2000);
} else {
    return addFriend;
}


所以在if语句中,我试图显示

<span className="small_font white_color border padding_5px horiz_center"
                      style={{marginTop: '-5px'}}>Amount changed</span>


第一部分,并在一些延迟后支付

<Retransfer reqUserId={reqUserId} changeAccountLevel={changeAccountLevel}/>


此组件。但是我的方法似乎不起作用。

谁能帮我这个?

最佳答案

您可以做的是在状态下有一个标志,该标志指示是显示还是不显示“重新传输”组件。像这样

state = {
    isVisibleRetransfer: false,
}
componentDidMount() {
    setTimeout(() => {
        this.setState({
            isVisibleRetransfer: true
        })
    }, 2000)
}


并在您的渲染中

if (success) {
    return
    <span className="small_font white_color border padding_5px horiz_center"
        style={{ marginTop: '-5px' }}>Amount changed</span>

    { this.state.isVisibleRetransfer && <Retransfer reqUserId={reqUserId} changeAccountLevel={changeAccountLevel} /> }
} else {
    return addFriend;

}


这是不可能的

关于javascript - 在返回函数中延迟一段时间后显示组件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48437664/

10-12 12:46
查看更多