2017-02-19 6 views
0

私は、htmlにリンクされたwebpackによって生成されたhtmlおよびbundle.jsファイルにリンクされた外部jsファイルを持っています。 index.htmlのコードでは外部javascriptファイル内の関数は、リロード時にのみ呼び出されます。react js

は問題がmain.jsの関数はWebPACKの実行後にreactjsでページのロードに一度だけ呼ばれ、コードが同梱されている

<script src="js/main.js"></script> 
<script type="text/javascript" src="bundle.min.js"></script>` 

です。

main.jsファイルのコード

(function() { 

    'use strict'; 

    var isMobile = { 
     Android: function() { 
      return navigator.userAgent.match(/Android/i); 
     }, 
      BlackBerry: function() { 
      return navigator.userAgent.match(/BlackBerry/i); 
     }, 
      iOS: function() { 
      return navigator.userAgent.match(/iPhone|iPad|iPod/i); 
     }, 
      Opera: function() { 
      return navigator.userAgent.match(/Opera Mini/i); 
     }, 
      Windows: function() { 
      return navigator.userAgent.match(/IEMobile/i); 
     }, 
      any: function() { 
      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); 
     } 
    }; 

    var mobileMenuOutsideClick = function() { 

     $(document).click(function (e) { 
     var container = $("#fh5co-offcanvas, .js-fh5co-nav-toggle"); 
     if (!container.is(e.target) && container.has(e.target).length === 0) { 

      if ($('body').hasClass('offcanvas')) { 

       $('body').removeClass('offcanvas'); 
       $('.js-fh5co-nav-toggle').removeClass('active'); 

      } 


     } 
     }); 

    }; 


    var offcanvasMenu = function() { 

     $('#page').prepend('<div id="fh5co-offcanvas" />'); 
     $('#page').prepend('<a href="#" class="js-fh5co-nav-toggle fh5co-nav-toggle fh5co-nav-white"><i></i></a>'); 
     var clone1 = $('.menu-1 > ul').clone(); 
     $('#fh5co-offcanvas').append(clone1); 
     var clone2 = $('.menu-2 > ul').clone(); 
     $('#fh5co-offcanvas').append(clone2); 

     $('#fh5co-offcanvas .has-dropdown').addClass('offcanvas-has-dropdown'); 
     $('#fh5co-offcanvas') 
      .find('li') 
      .removeClass('has-dropdown'); 

     // Hover dropdown menu on mobile 
     $('.offcanvas-has-dropdown').mouseenter(function(){ 
      var $this = $(this); 

      $this 
       .addClass('active') 
       .find('ul') 
       .slideDown(500, 'easeOutExpo'); 
     }).mouseleave(function(){ 

      var $this = $(this); 
      $this 
       .removeClass('active') 
       .find('ul') 
       .slideUp(500, 'easeOutExpo'); 
     }); 


     $(window).resize(function(){ 

      if ($('body').hasClass('offcanvas')) { 

       $('body').removeClass('offcanvas'); 
       $('.js-fh5co-nav-toggle').removeClass('active'); 

      } 
     }); 
    }; 


    var burgerMenu = function() { 

     $('body').on('click', '.js-fh5co-nav-toggle', function(event){ 
      var $this = $(this); 


      if ($('body').hasClass('overflow offcanvas')) { 
       $('body').removeClass('overflow offcanvas'); 
      } else { 
       $('body').addClass('overflow offcanvas'); 
      } 
      $this.toggleClass('active'); 
      event.preventDefault(); 

     }); 
    }; 

    var fullHeight = function() { 

     if (!isMobile.any()) { 
      $('.js-fullheight').css('height', $(window).height()); 
      $(window).resize(function(){ 
       $('.js-fullheight').css('height', $(window).height()); 
      }); 
     } 

    }; 



    var contentWayPoint = function() { 
     var i = 0; 
     $('.animate-box').waypoint(function(direction) { 

      if(direction === 'down' && !$(this.element).hasClass('animated-fast')) { 

       i++; 

       $(this.element).addClass('item-animate'); 
       setTimeout(function(){ 

        $('body .animate-box.item-animate').each(function(k){ 
         var el = $(this); 
         setTimeout(function() { 
          var effect = el.data('animate-effect'); 
          if (effect === 'fadeIn') { 
           el.addClass('fadeIn animated-fast'); 
          } else if (effect === 'fadeInLeft') { 
           el.addClass('fadeInLeft animated-fast'); 
          } else if (effect === 'fadeInRight') { 
           el.addClass('fadeInRight animated-fast'); 
          } else { 
           el.addClass('fadeInUp animated-fast'); 
          } 

          el.removeClass('item-animate'); 
         }, k * 200, 'easeInOutExpo'); 
        }); 

       }, 100); 

      } 

     } , { offset: '85%' }); 
    }; 


    var dropdown = function() { 

     $('.has-dropdown').mouseenter(function(){ 

      var $this = $(this); 
      $this 
       .find('.dropdown') 
       .css('display', 'block') 
       .addClass('animated-fast fadeInUpMenu'); 

     }).mouseleave(function(){ 
      var $this = $(this); 

      $this 
       .find('.dropdown') 
       .css('display', 'none') 
       .removeClass('animated-fast fadeInUpMenu'); 
     }); 

    }; 


    var goToTop = function() { 

     $('.js-gotop').on('click', function(event){ 

      event.preventDefault(); 

      $('html, body').animate({ 
       scrollTop: $('html').offset().top 
      }, 500, 'easeInOutExpo'); 

      return false; 
     }); 

     $(window).scroll(function(){ 

      var $win = $(window); 
      if ($win.scrollTop() > 200) { 
       $('.js-top').addClass('active'); 
      } else { 
       $('.js-top').removeClass('active'); 
      } 

     }); 

    }; 


    // Loading page 
    var loaderPage = function() { 
     $(".fh5co-loader").fadeOut("slow"); 
    }; 

    var counter = function() { 
     $('.js-counter').countTo({ 
      formatter: function (value, options) { 
      return value.toFixed(options.decimals); 
     }, 
     }); 
    }; 

    var counterWayPoint = function() { 
     if ($('#fh5co-counter').length > 0) { 
      $('#fh5co-counter').waypoint(function(direction) { 

       if(direction === 'down' && !$(this.element).hasClass('animated')) { 
        setTimeout(counter , 400); 
        $(this.element).addClass('animated'); 
       } 
      } , { offset: '90%' }); 
     } 
    }; 

    var parallax = function() { 

     if (!isMobile.any()) { 
      $(window).stellar({ 
       horizontalScrolling: false, 
       hideDistantElements: false, 
       responsive: true 

      }); 
     } 
    }; 

    var testimonialCarousel = function(){ 

     var owl = $('.owl-carousel-fullwidth'); 
     owl.owlCarousel({ 
      items: 1, 
      loop: true, 
      margin: 0, 
      nav: false, 
      dots: true, 
      smartSpeed: 800, 
      autoHeight: true 
     }); 
    }; 

    var sliderMain = function() { 

     $('#fh5co-hero .flexslider').flexslider({ 
      animation: "fade", 
      slideshowSpeed: 5000, 
      directionNav: true, 
      start: function(){ 
       setTimeout(function(){ 
        $('.slider-text').removeClass('animated fadeInUp'); 
        $('.flex-active-slide').find('.slider-text').addClass('animated fadeInUp'); 
       }, 500); 
      }, 
      before: function(){ 
       setTimeout(function(){ 
        $('.slider-text').removeClass('animated fadeInUp'); 
        $('.flex-active-slide').find('.slider-text').addClass('animated fadeInUp'); 
       }, 500); 
      } 

     }); 

     $('#fh5co-hero .flexslider .slides > li').css('height', $(window).height()); 
     $(window).resize(function(){ 
      $('#fh5co-hero .flexslider .slides > li').css('height', $(window).height()); 
     }); 

    }; 


    $(function(){ 
     mobileMenuOutsideClick(); 
     offcanvasMenu(); 
     burgerMenu(); 
     contentWayPoint(); 
     sliderMain(); 
     dropdown(); 
     goToTop(); 
     loaderPage(); 
     counterWayPoint(); 
     counter(); 
     parallax(); 
     testimonialCarousel(); 
     fullHeight(); 
    }); 


}()); 

である私はリロードだけでなく、常に呼ばmain.jsに私のコードをしたいです。私はreactjsとwebpackを使用しています。 これを解決してください。ありがとうございました。

+0

あなたの機能をたとえば、Utils ...(モジュール)として追加することを検討しましたか?このようにして、オンデマンドで実行することができます。 –

答えて

1

あなたmain.jsでコードがこのような何かに包まれていることに注意してください:建設上記

(function() { 
    ... 
}()); 

Immediately-Invoked Function Expression (IIFE)と呼ばれる - それはすぐに呼ばれるように内部のコードを作成し、任意の関数内で定義されたすべてのものにのみ表示されているためこの関数では(関数スコープの外部には表示されません)、グローバルコンテキストを汚染しないようにも役立ちます。あなたはそれについてもっと読むことができますhere。 あなたはいつでもそれを呼び出すための可能性を持っているしたい場合は、あなたが通常の関数(ないIIFE)を使用する必要があります必要があります。

function mainFunction() { 
    //your codde 
} 

をして、あなたはいつでもあなたが(ページのロードにも)必要がある、それを呼び出すことができます。

+0

関数を呼び出す場所を教えてください。反応成分または他の場所。 – Goutham

+1

まあ、それは本当にあなたのアプリケーションに依存します。あなたの関数は多くのDOM要素にアクセスするので、それらの要素があなたのページで利用可能なときに呼び出さなければなりませんが、それはあなたのReactコンポーネントロジックに依存します。私は一般的には、プラグイン(owlCarousel、flexslider ..)をそれぞれ関連するマークアップを担当するコンポーネントの内部で初期化することをお勧めします。あなたは 'componentDidMount'と' componentDidUpdate'フックでそれらを呼び出すことができます –

関連する問題