Jamming REACT (general question)

Why the original codecademy example of JAMMING which is here

Starts working after the second search click ?
I thought it was only my bug :roll_eyes:

1 Like

Hi Yves!
There is this thread here in the forum:
https://discuss.codecademy.com/t/jammming-page-refresh-after-first-song-search/463075/2

But I thought that this happens because acces token is not required until you click search the first time, which it could be solved by calling to search() when it loads the page, but donโ€™t know how to do it, and Iโ€™m not sure this is the reason.
Anyway we may solve it while working in the next step Feature Request: Jammming

Iโ€™ve just open this thread on the forum to share our work
https://discuss.codecademy.com/t/feature-request-jammming/486290

Blockquote
happy coding! :stuck_out_tongue_winking_eye:

1 Like

You can run an empty search the first time, which will reload the page and allow you to search with no problems. Knowing this, I chose to force an empty search automatically when the page loads by adding the following code to the App component:

 componentDidMount() {
    window.addEventListener('load', Spotify.search(''));
  }

This has the added benefit of bringing up the login screen automatically when the page loads if you are not already logged in.

4 Likes

I found an easyer solution placing Spotify.getAccessToken(); in App.js just after the imports.
Thank you anyway.

2 Likes

Hello! The search triggers an event and you can use event.preventDefault() in your event handler.

Hope that helps!