【问题标题】:React JS Custom Component AnimationReact JS 自定义组件动画
【发布时间】:2018-08-26 20:43:18
【问题描述】:

我有一个看起来像这样的 ReactJS 组件:

import React from 'react';

class Circle extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            error: null,
            isLoaded: false,
            percent: null
        };
    }    

    componentDidMount() {
        const url = "base url here" + this.props.endpoint;
        fetch(url)
            .then(res => res.json())
            .then(
                (result) => {
                    this.setState({
                        isLoaded: true,
                        percent: result.percent
                    });
                },
                (error) => {
                    this.setState({
                        isLoaded: true,
                        error: error
                    });
                }
            )
    }

    render() {
        return (
            <div className={"col-md-" + this.props.size + " progress-circles"} data-animate-on-scroll="on">
                <div className="progress-circle" data-circle-percent={"" + this.state.percent + ""} data-circle-text="">
                <h4>{this.props.title}</h4>
                    <svg className="progress-svg">
                        <circle r="80" cx="90" cy="90" fill="transparent" strokeDasharray="502.4" strokeDashoffset="0"></circle>
                        <circle className="bar" r="80" cx="90" cy="90" fill="transparent" strokeDasharray="502.4" strokeDashoffset="0"></circle>
                    </svg>
                </div>
            </div>
        );
    }
}

export default Circle;

到目前为止,这就像一个完美的魅力。唯一的问题是,有一个与该元素关联的动画填充进度圈,并且在我从componentDidMount 函数设置值后它不会发生。如果我通过父组件的道具设置值,动画确实会发生。我错过了什么吗?我是 React 的新手,所以它可能很简单。提前致谢!

【问题讨论】:

    标签: javascript html reactjs animation


    【解决方案1】:

    您的解决方案的问题是,无论您是否进行 fetch 调用,您总是在渲染动画。

    在解决之前我想在这里解释几件事

    1. 在 componentDidMount 中的 fetch 调用之前将 isLoaded 设置为 true
    2. 当您获得成功/错误响应时,将 isLoaded 设置为 false
    3. 当您获得成功响应时,您还应该将错误状态更改为 null 以使其第二次工作
    4. 当您收到错误响应时,您还应该将百分比状态更改为 null 以使其第二次工作
    5. 当 isLoaded 为 true、error 和 percent 均为 null 时显示加载器
    6. 当百分比不为空、isLoaded 为假且错误为空时显示成功响应
    7. 当 error 不为 null、isLoaded false 且 percent 为 null 时显示错误响应

    这是我在应用程序中通常的做法

    import React from 'react';
    
    class Circle extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                error: null,
                isLoaded: false,
                percent: null
            };
        }    
    
        componentDidMount() {
            const url = "base url here" + this.props.endpoint;
            this.setState({
                isLoaded: true
            });
            fetch(url)
                .then(res => res.json())
                .then(
                    (result) => {
                        this.setState({
                            isLoaded: false,
                            percent: result.percent,
                            error: null
                        });
                    },
                    (error) => {
                        this.setState({
                            isLoaded: false,
                            error: error,
                            percent: null
                        });
                    }
                )
        }
    
        render() {
            const { isLoaded } = this.state;
            return (
                <div className={"col-md-" + this.props.size + " progress-circles"} data-animate-on-scroll="on">
                    {isLoaded && percent == null && error == null && (<div className="progress-circle" data-circle-percent={"" + this.state.percent + ""} data-circle-text="">
                    <h4>{this.props.title}</h4>
                        <svg className="progress-svg">
                            <circle r="80" cx="90" cy="90" fill="transparent" strokeDasharray="502.4" strokeDashoffset="0"></circle>
                            <circle className="bar" r="80" cx="90" cy="90" fill="transparent" strokeDasharray="502.4" strokeDashoffset="0"></circle>
                        </svg>
                    </div>)}
                    {!isLoaded && percent != null && <h2>Success response</h2>}
                    {!isLoaded && error != null && <h2>error occured</h2>}
                </div>
            );
        }}
    
        export default Circle;
    

    希望这能回答您的问题

    【讨论】:

    • 嗨!这很有帮助,但我想我发现了问题的根源——动画是由加载时在scripts.js 文件中调用的函数触发的。所以我认为它只是缺少那里的元素,因为它在脚本运行后加载。有没有办法再次重新触发任意 javascript 函数?我目前已通过index.html 将其导入页面,该文件位于我的Public 文件夹中。
    • 分享你的 index.html 文件
    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 2015-07-14
    • 1970-01-01
    • 2023-01-14
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多