【问题标题】:How to Load all fetch from react before loading custom javascript from separate file如何在从单独的文件加载自定义 javascript 之前从反应中加载所有提取
【发布时间】:2019-05-25 13:57:39
【问题描述】:

我同时使用 react 和 jquery 我希望 bundle.js 在运行 jquery 函数之前从 react 获取所有 api 数据。

我有一个反应代码从服务器 / api 和 jquery 加载所有图像以滑动这些图像并管理图像的高度以适合屏幕。 我首先包含了 bundle.js,然后是我的 custom.js 文件。即使 bundle.js 先运行,但是 fetch api 在 custom.js 之后运行,因为 jquery 无法滑动这些图像。

App.js

export class App extends Component {

constructor(props){
    super(props)
    this.state = {
        loading : false,
        all_data : []
    }
    this.fetchFromServer();
}

fetchFromServer(){
    Promise.all([
        fetch("http://abcdef.com/api/slider.php",{
            method : "POST",
            headers: new Headers({
                'Accept' : 'application/json',
            }),
            body: JSON.stringify({
                limit : "10",
            })
        })
    ]).then(([all_post_api])=>Promise.all([all_post_api.json()])).
    then(([post_data]) => this.setState({
        loading : false,
        all_data : post_data
    }))
}

render(){
        const allowRender = this.state.all_data && this.state.all_data.carousel
        if (allowRender) {

        return (<div className="right-content">

            <Main data={this.state.all_data} />
        </div>)

        } 

        return null;
}

}

Main.js

export class Main extends Component {

render(){
    console.log(this.props.data.carousel);
    return (


                <div className="flexslider home-page">
                    <ul className="slides">
                    {
                        this.props.data.carousel.map((name,index)=>{
                            return (
                            <li className="images-bg" key={index}>
                                <img src={name.url} />
                                <div className="circle">
                                            <div className="home-title left">
                                                <span>
                                                    Welcome <strong>to</strong>
                                                </span>
                                                <span>
                                                    <strong>foto</strong>hunter
                                                </span>
                                                <p>Only the best pictures of animals and travel.</p>
                                            </div>
                                            <a className="button-down glyph fa-angle-down" href=""></a>
                                        </div>
                            </li>
                            )

                        })

                    }
                    </ul>
                </div>




    )
}

}

custom.js

$(function (){        
 console.log("loaded content");
$('.images-bg').each(function(){
            $(this).css({
                'background-image': 'url(' +$('img', this).hide().attr('src') +')',
                'height': $(window).height()
            });
        });

        /*----------  BIG SLIDER  ----------*/
        $('.portfolio-with-details .flexslider, .service .flexslider').flexslider({slideshowSpeed: 5000});
        $('.portfolio-image.flexslider').flexslider({slideshow: false});
        $('.flexslider.home-page').flexslider({
            slideshowSpeed: 5000,
            after : function(slider){
                var next = $('.flex-active-slide', slider).find('.home-title');
                var className = '';
                if(next.hasClass('left')){
                    className = 'bounceInLeft';
                }else if(next.hasClass('top')){
                    className = 'flipInX';
                }else if(next.hasClass('zoom')){
                    className = 'bounceIn';
                }
                next.addClass(className + ' animated');
                next.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
                    next.removeClass(className + ' animated');
                });
            }
        });
        /*----------  //BIG SLIDER  ----------*/

    })

下面的截图是我现在得到的。我要相反。我希望在 Main.js console.log 之后打印 custom.js console.log 消息

【问题讨论】:

    标签: javascript jquery reactjs dom fetch


    【解决方案1】:

    在您的 Jquery 中尝试将其更改为

    $( document ).ready(function (){        
     console.log("loaded content");
    $('.images-bg').each(function(){
                $(this).css({
                    'background-image': 'url(' +$('img', this).hide().attr('src') +')',
                    'height': $(window).height()
                });
            });
    
            /*----------  BIG SLIDER  ----------*/
            $('.portfolio-with-details .flexslider, .service .flexslider').flexslider({slideshowSpeed: 5000});
            $('.portfolio-image.flexslider').flexslider({slideshow: false});
            $('.flexslider.home-page').flexslider({
                slideshowSpeed: 5000,
                after : function(slider){
                    var next = $('.flex-active-slide', slider).find('.home-title');
                    var className = '';
                    if(next.hasClass('left')){
                        className = 'bounceInLeft';
                    }else if(next.hasClass('top')){
                        className = 'flipInX';
                    }else if(next.hasClass('zoom')){
                        className = 'bounceIn';
                    }
                    next.addClass(className + ' animated');
                    next.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
                        next.removeClass(className + ' animated');
                    });
                }
            });
            /*----------  //BIG SLIDER  ----------*/
    
        })
    

    这样,React 会在挂载之前获取,而 Jquery 会在页面完全准备好时触发!

    【讨论】:

    • 即使使用 componentWillMount 也会产生相同的输出。我想要的是“加载的内容”应该在 {carousel: Array(3)} 之后打印
    • 不过,在您的位置上,我会尝试将您的 Jquery 转换为 React。它导致更少的问题。希望这个解决方案虽然有效
    • 即使进行了上述更改,我也得到了相同的结果。但是,在构造时,我的加载状态为 false,但在 componentWillMount 内部,我将加载更改为 true,而不是使用 fetchFromServer() 方法,结果是 Loading : true, {} Read! (来自 jquery)Loading: false, {Carousel:Array(3)} 为什么 all_data 状态没有改变任何想法?
    • 尝试删除组件willmount 并检查你的jquery stackoverflow.com/questions/9036969/…
    • 我正在尝试将我的 Jquery 转换为暂时做出反应。让我们看看它是否有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 2015-08-22
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多