2

I'm trying Gatsby for the first time to build a blog. I'm using the Gatsby Starter Blog as a base.

I want to add some javascript to animate some text, and just as a quick test I've added the below to my html.js file:

<script
      dangerouslySetInnerHTML={{
        __html: `
                var name = 'world';
                console.log('Hello ' + name);

                console.log('hey');

                const phrases = [
                    'aaa',
                    'bbb',
                    'ccc'
                ];

                const el = document.getElementById('bio__subtext');

                el.innerHTML = 'hi';

            `,
      }}
    />

However this is throwing the following error in my console: (index):15 Uncaught TypeError: Cannot read property 'innerHTML' of null

The ID is correct, and exists in my Bio.js file:

class Bio extends React.Component {
  render() {
    return (
      <div id="bio">
        <div className="bio_wrapper">
            <img
              src={profilePic}
              alt={'NdW'}
            />
            <p>
              Hi, I'm Natalie!<br />
              I <span id="bio__subtext"></span><br/>
              <span className="about_button"><a href="/">Find out more</a></span>
              <a href="#" target="_blank">GH</a>
              <a href="#" target="_blank">TW</a>
            </p>
        </div>
      </div>
    )
  }
}

I assume this is because the JS is trying to run before Bio.js has finished.. but how do I tell the JS to "wait"? If I try including the JS in the Bio.js file, it fails to compile.

Adding document.addEventListener("DOMContentLoaded", function(event) { doesn't help either.

5
  • 1
    just a hint const el should be let el or var el. modifying constant is not correct Commented Dec 20, 2018 at 16:32
  • What exactly are you trying to achieve? Commented Dec 20, 2018 at 16:45
  • @ksav At the moment, I'm just trying to replace the text within #bio__subtext. Eventually I'll be looping through an array of phrases and animating between the stages. Commented Dec 20, 2018 at 16:46
  • @ksav How? Everything I try seems to cause a compile error... Commented Dec 20, 2018 at 16:49
  • I meant to say: in Bio you can add some javascript before the render() function's return statement :) Commented Dec 20, 2018 at 16:53

1 Answer 1

1

There shouldn't be any need to select anything in the DOM like you are attempting with the use of document.getElementById('bio__subtext').

You can declare variables right in your component where they are needed or pass then into the component via a prop from a higher context (a page or another component).

class Bio extends React.Component {
  render () {
    const name = 'Natalie'
    const phrases = [
      'Cowabunga',
      'Its Ninja Time',
      'Booyakasha'
    ]
    return (
      <div id="bio">
        Hi, I'm {name}!<br />
        Some of my favourite phrases are:
        <ul>
          {phrases.map(phrase => <li>{phrase}</li>)}
        </ul>
      </div>
    )
  }
}

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for this; however I want to animate between the phrases, rather than just print them out. Something similar to this: codepen.io/soulwire/pen/mErPAK
Ok but I think you need to walk before you can run. Work on figuring out the basics of structuring your app and passing data around before asking another more specific question here on stackoverflow.
So I declare my phrases as you have, and then pass them into the JS?
@Natalie the official react tutorial is an excellent resource, the part about state & life cycle will be of your interest reactjs.org/docs/hello-world.html
You could create an array of phrases and pass them to a TextScrambler component. Within that component you would have access to the the array via props. Then you could write the text scramble logic inside that component.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.