Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
How do you use data that was passed into a component in a function?

I have a functional component that has been passed data ({clicked}) from its parent function, but when I try to use it as a variable in my function, it errors out. Here is my current code:

function Songs({clicked}) {

    const songListFiltered = songList.filter(item => item.{clicked} === true) 
    const songDisplayed = songListFiltered[Math.floor(Math.random() * songListFiltered.length)] 
    return (
        <div className="song-box">
            <img src={songDisplayed.artwork} alt="album artwork" />
            <h3>{songDisplayed.songName}</h3>
            <p>{songDisplayed.artistName}</p>
        </div>
    )
}

I need to check if songList has the data in {clicked}, in order to filter those songs from the list, but I'm not sure how to make it work. Would love any help!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Depending on the shape of your data, you would invoke this different ways.
The variable {clicked} would be an object with key value pairs, so you would need to use it as clicked.yourCustomValue. If it's an array, you would use the index, so item.clicked[0], and if clicked is a function, you would need to invoke it with clicked().

about 4 years ago · Juan Pablo Isaza Report

0

Based on the statement

I need to check if songList has the data in {clicked}

And based on the code

songList.filter(item => item.{clicked} === true)

i assume that the variable {clicked} would be an array containing a collection of song objects that have been selected by the user, and the data type of the songList variable is an array containing song objects

So, you can replace the variable songListFiltered with

const songListFiltered = songList.filter(function (e) {
    let result = [];
    for (let songClicked of clicked) {
      if (e.songName === songClicked.songName) {
        result.push(e);
      }
    }
    return result.length === 0 ? null : result;
  });

Or if you mean {clicked} is a property that you add to the each of songList object when you have selected a song, then you can use typeof when checking the condition filter. Then, the code could be as follows songList.filter(item => typeof(item.clicked) !== "undefined")

Array filtering element Array contains check

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!