How to add js to React components in Gatsby?









up vote
0
down vote

favorite












I'm trying to add the scroll function in script tags to this header component in Gatsby. I know it could work in html and not in react, but what is the right way to do it? Thanks!






import React from 'react'
import Link from 'gatsby-link'
import './header.css'

const Header = () => (
<div className='Header'>

<div className='HeaderGroup'>
<Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
<Link to='/index'>Selected Works</Link>
<Link to='/uber'>Uber Thoughts</Link>
<Link to='/awards'>Awards</Link>
<Link to='/about'>About</Link>
</div>
</div>
)

export default Header

<script>
$(window).scroll(function ()
if ($(window).scrollTop() > 10)
$('.Header').addClass('floatingHeader');
else
$('.Header').removeClass('floatingHeader');


</script>












share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm trying to add the scroll function in script tags to this header component in Gatsby. I know it could work in html and not in react, but what is the right way to do it? Thanks!






    import React from 'react'
    import Link from 'gatsby-link'
    import './header.css'

    const Header = () => (
    <div className='Header'>

    <div className='HeaderGroup'>
    <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
    <Link to='/index'>Selected Works</Link>
    <Link to='/uber'>Uber Thoughts</Link>
    <Link to='/awards'>Awards</Link>
    <Link to='/about'>About</Link>
    </div>
    </div>
    )

    export default Header

    <script>
    $(window).scroll(function ()
    if ($(window).scrollTop() > 10)
    $('.Header').addClass('floatingHeader');
    else
    $('.Header').removeClass('floatingHeader');


    </script>












    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to add the scroll function in script tags to this header component in Gatsby. I know it could work in html and not in react, but what is the right way to do it? Thanks!






      import React from 'react'
      import Link from 'gatsby-link'
      import './header.css'

      const Header = () => (
      <div className='Header'>

      <div className='HeaderGroup'>
      <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
      <Link to='/index'>Selected Works</Link>
      <Link to='/uber'>Uber Thoughts</Link>
      <Link to='/awards'>Awards</Link>
      <Link to='/about'>About</Link>
      </div>
      </div>
      )

      export default Header

      <script>
      $(window).scroll(function ()
      if ($(window).scrollTop() > 10)
      $('.Header').addClass('floatingHeader');
      else
      $('.Header').removeClass('floatingHeader');


      </script>












      share|improve this question













      I'm trying to add the scroll function in script tags to this header component in Gatsby. I know it could work in html and not in react, but what is the right way to do it? Thanks!






      import React from 'react'
      import Link from 'gatsby-link'
      import './header.css'

      const Header = () => (
      <div className='Header'>

      <div className='HeaderGroup'>
      <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
      <Link to='/index'>Selected Works</Link>
      <Link to='/uber'>Uber Thoughts</Link>
      <Link to='/awards'>Awards</Link>
      <Link to='/about'>About</Link>
      </div>
      </div>
      )

      export default Header

      <script>
      $(window).scroll(function ()
      if ($(window).scrollTop() > 10)
      $('.Header').addClass('floatingHeader');
      else
      $('.Header').removeClass('floatingHeader');


      </script>








      import React from 'react'
      import Link from 'gatsby-link'
      import './header.css'

      const Header = () => (
      <div className='Header'>

      <div className='HeaderGroup'>
      <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
      <Link to='/index'>Selected Works</Link>
      <Link to='/uber'>Uber Thoughts</Link>
      <Link to='/awards'>Awards</Link>
      <Link to='/about'>About</Link>
      </div>
      </div>
      )

      export default Header

      <script>
      $(window).scroll(function ()
      if ($(window).scrollTop() > 10)
      $('.Header').addClass('floatingHeader');
      else
      $('.Header').removeClass('floatingHeader');


      </script>





      import React from 'react'
      import Link from 'gatsby-link'
      import './header.css'

      const Header = () => (
      <div className='Header'>

      <div className='HeaderGroup'>
      <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
      <Link to='/index'>Selected Works</Link>
      <Link to='/uber'>Uber Thoughts</Link>
      <Link to='/awards'>Awards</Link>
      <Link to='/about'>About</Link>
      </div>
      </div>
      )

      export default Header

      <script>
      $(window).scroll(function ()
      if ($(window).scrollTop() > 10)
      $('.Header').addClass('floatingHeader');
      else
      $('.Header').removeClass('floatingHeader');


      </script>






      reactjs gatsby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 22:29









      Muchao

      31




      31






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          If you want scripts to load before the DOM is ready you can add your scripts inside html.js file.



          From the Gatsby docs:




          Gatsby uses a React component to server render the and other
          parts of the HTML outside of the core Gatsby application.




          Read more about it here.



          In your case, what you can do is to write your script inside the componentDidMount react lifecycle method, because you need access to the DOM (as you're using jQuery there) you need to run the script after the body has been loaded, so placing your script in the <head> won't work, you need to add it inside the componentDidMount method by first making your component a class component to get access to the react lifecycle methods.



          import React from 'react'
          import Link from 'gatsby-link'
          import $ from 'jquery'

          import './header.css'

          class Header extends React.Component
          componentDidMount ()
          $(window).scroll(function ()
          if ($(window).scrollTop() > 10)
          $('.Header').addClass('floatingHeader');
          else
          $('.Header').removeClass('floatingHeader');

          )

          render ()
          return (
          <div className='Header'>
          <div className='HeaderGroup'>
          <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
          <Link to='/index'>Selected Works</Link>
          <Link to='/uber'>Uber Thoughts</Link>
          <Link to='/awards'>Awards</Link>
          <Link to='/about'>About</Link>
          </div>
          </div>
          )



          export default Header


          You can also use a Gatsby layout template like the gatsby-starter-blog project and put your script at the bottom of the children call as a <script>Your script</script> and it will be available in all your pages, same as using the html.js file but since you need access to the DOM you need to put it inside the body for your script to work (more info about Gatsby layouts here).






          share|improve this answer






















          • Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
            – Muchao
            Nov 10 at 23:16










          • I updated the answer, you had a typo in your script function :)
            – bntzio
            Nov 10 at 23:31










          • Thanks so much it works!
            – Muchao
            Nov 11 at 20:23










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244062%2fhow-to-add-js-to-react-components-in-gatsby%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          If you want scripts to load before the DOM is ready you can add your scripts inside html.js file.



          From the Gatsby docs:




          Gatsby uses a React component to server render the and other
          parts of the HTML outside of the core Gatsby application.




          Read more about it here.



          In your case, what you can do is to write your script inside the componentDidMount react lifecycle method, because you need access to the DOM (as you're using jQuery there) you need to run the script after the body has been loaded, so placing your script in the <head> won't work, you need to add it inside the componentDidMount method by first making your component a class component to get access to the react lifecycle methods.



          import React from 'react'
          import Link from 'gatsby-link'
          import $ from 'jquery'

          import './header.css'

          class Header extends React.Component
          componentDidMount ()
          $(window).scroll(function ()
          if ($(window).scrollTop() > 10)
          $('.Header').addClass('floatingHeader');
          else
          $('.Header').removeClass('floatingHeader');

          )

          render ()
          return (
          <div className='Header'>
          <div className='HeaderGroup'>
          <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
          <Link to='/index'>Selected Works</Link>
          <Link to='/uber'>Uber Thoughts</Link>
          <Link to='/awards'>Awards</Link>
          <Link to='/about'>About</Link>
          </div>
          </div>
          )



          export default Header


          You can also use a Gatsby layout template like the gatsby-starter-blog project and put your script at the bottom of the children call as a <script>Your script</script> and it will be available in all your pages, same as using the html.js file but since you need access to the DOM you need to put it inside the body for your script to work (more info about Gatsby layouts here).






          share|improve this answer






















          • Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
            – Muchao
            Nov 10 at 23:16










          • I updated the answer, you had a typo in your script function :)
            – bntzio
            Nov 10 at 23:31










          • Thanks so much it works!
            – Muchao
            Nov 11 at 20:23














          up vote
          0
          down vote



          accepted










          If you want scripts to load before the DOM is ready you can add your scripts inside html.js file.



          From the Gatsby docs:




          Gatsby uses a React component to server render the and other
          parts of the HTML outside of the core Gatsby application.




          Read more about it here.



          In your case, what you can do is to write your script inside the componentDidMount react lifecycle method, because you need access to the DOM (as you're using jQuery there) you need to run the script after the body has been loaded, so placing your script in the <head> won't work, you need to add it inside the componentDidMount method by first making your component a class component to get access to the react lifecycle methods.



          import React from 'react'
          import Link from 'gatsby-link'
          import $ from 'jquery'

          import './header.css'

          class Header extends React.Component
          componentDidMount ()
          $(window).scroll(function ()
          if ($(window).scrollTop() > 10)
          $('.Header').addClass('floatingHeader');
          else
          $('.Header').removeClass('floatingHeader');

          )

          render ()
          return (
          <div className='Header'>
          <div className='HeaderGroup'>
          <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
          <Link to='/index'>Selected Works</Link>
          <Link to='/uber'>Uber Thoughts</Link>
          <Link to='/awards'>Awards</Link>
          <Link to='/about'>About</Link>
          </div>
          </div>
          )



          export default Header


          You can also use a Gatsby layout template like the gatsby-starter-blog project and put your script at the bottom of the children call as a <script>Your script</script> and it will be available in all your pages, same as using the html.js file but since you need access to the DOM you need to put it inside the body for your script to work (more info about Gatsby layouts here).






          share|improve this answer






















          • Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
            – Muchao
            Nov 10 at 23:16










          • I updated the answer, you had a typo in your script function :)
            – bntzio
            Nov 10 at 23:31










          • Thanks so much it works!
            – Muchao
            Nov 11 at 20:23












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          If you want scripts to load before the DOM is ready you can add your scripts inside html.js file.



          From the Gatsby docs:




          Gatsby uses a React component to server render the and other
          parts of the HTML outside of the core Gatsby application.




          Read more about it here.



          In your case, what you can do is to write your script inside the componentDidMount react lifecycle method, because you need access to the DOM (as you're using jQuery there) you need to run the script after the body has been loaded, so placing your script in the <head> won't work, you need to add it inside the componentDidMount method by first making your component a class component to get access to the react lifecycle methods.



          import React from 'react'
          import Link from 'gatsby-link'
          import $ from 'jquery'

          import './header.css'

          class Header extends React.Component
          componentDidMount ()
          $(window).scroll(function ()
          if ($(window).scrollTop() > 10)
          $('.Header').addClass('floatingHeader');
          else
          $('.Header').removeClass('floatingHeader');

          )

          render ()
          return (
          <div className='Header'>
          <div className='HeaderGroup'>
          <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
          <Link to='/index'>Selected Works</Link>
          <Link to='/uber'>Uber Thoughts</Link>
          <Link to='/awards'>Awards</Link>
          <Link to='/about'>About</Link>
          </div>
          </div>
          )



          export default Header


          You can also use a Gatsby layout template like the gatsby-starter-blog project and put your script at the bottom of the children call as a <script>Your script</script> and it will be available in all your pages, same as using the html.js file but since you need access to the DOM you need to put it inside the body for your script to work (more info about Gatsby layouts here).






          share|improve this answer














          If you want scripts to load before the DOM is ready you can add your scripts inside html.js file.



          From the Gatsby docs:




          Gatsby uses a React component to server render the and other
          parts of the HTML outside of the core Gatsby application.




          Read more about it here.



          In your case, what you can do is to write your script inside the componentDidMount react lifecycle method, because you need access to the DOM (as you're using jQuery there) you need to run the script after the body has been loaded, so placing your script in the <head> won't work, you need to add it inside the componentDidMount method by first making your component a class component to get access to the react lifecycle methods.



          import React from 'react'
          import Link from 'gatsby-link'
          import $ from 'jquery'

          import './header.css'

          class Header extends React.Component
          componentDidMount ()
          $(window).scroll(function ()
          if ($(window).scrollTop() > 10)
          $('.Header').addClass('floatingHeader');
          else
          $('.Header').removeClass('floatingHeader');

          )

          render ()
          return (
          <div className='Header'>
          <div className='HeaderGroup'>
          <Link to='/'><img src=require('../img/logo_nav.png') width='60' /></Link>
          <Link to='/index'>Selected Works</Link>
          <Link to='/uber'>Uber Thoughts</Link>
          <Link to='/awards'>Awards</Link>
          <Link to='/about'>About</Link>
          </div>
          </div>
          )



          export default Header


          You can also use a Gatsby layout template like the gatsby-starter-blog project and put your script at the bottom of the children call as a <script>Your script</script> and it will be available in all your pages, same as using the html.js file but since you need access to the DOM you need to put it inside the body for your script to work (more info about Gatsby layouts here).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 23:30

























          answered Nov 10 at 22:48









          bntzio

          574520




          574520











          • Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
            – Muchao
            Nov 10 at 23:16










          • I updated the answer, you had a typo in your script function :)
            – bntzio
            Nov 10 at 23:31










          • Thanks so much it works!
            – Muchao
            Nov 11 at 20:23
















          • Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
            – Muchao
            Nov 10 at 23:16










          • I updated the answer, you had a typo in your script function :)
            – bntzio
            Nov 10 at 23:31










          • Thanks so much it works!
            – Muchao
            Nov 11 at 20:23















          Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
          – Muchao
          Nov 10 at 23:16




          Thanks! But it shows that the last } of the componentDidMount method is an unexpected token
          – Muchao
          Nov 10 at 23:16












          I updated the answer, you had a typo in your script function :)
          – bntzio
          Nov 10 at 23:31




          I updated the answer, you had a typo in your script function :)
          – bntzio
          Nov 10 at 23:31












          Thanks so much it works!
          – Muchao
          Nov 11 at 20:23




          Thanks so much it works!
          – Muchao
          Nov 11 at 20:23

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244062%2fhow-to-add-js-to-react-components-in-gatsby%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin