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

179
Views
Showing the element's name or emoji on click in React, getting undefined

Here is my component:

const Card = ({ _name, emoji, logger }) => {
  return (
    <div style={styles.wrapper}>
      <button onClick={logger}>-</button>
      <h4>
        {_name} {emoji}
      </h4>
    </div>
  );
};

And here is my App.js:

const arr = [
  { _name: 'Apple', emoji: '๐ŸŽ', id: '01' },
  { _name: 'Banana', emoji: '๐ŸŒ', id: '02' },
  { _name: 'Peach', emoji: '๐Ÿ‘', id: '03' },
  { _name: 'Pineapple', emoji: '๐Ÿ', id: '04' },
  { _name: 'Mango', emoji: '๐Ÿฅญ', id: '05' },
  { _name: 'Melon', emoji: '๐Ÿ‰', id: '06' },
];

const App = () => {
  const logger = (el) => {
    alert(el.target.value.emoji);
  };

  return (
    <div style={styles.wrapper}>
      {arr.map((item) => {
        return (
          <Card
            _name={item._name}
            emoji={item.emoji}
            key={item.id}
            logger={logger}
          />
        );
      })}
    </div>
  );
};

I'm trying to show the element emoji when you click on it. When I click on each item I got undefined. Why is that? What is the problem?

about 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

The parameter that gets passed to your click handler dosent't have a target.value.emoji, that's why you are getting undefined.

One way to do what you want is to change logger to this:

const logger = (emoji) => {
  alert(emoji);
};

And Card component to:

const Card = ({ _name, emoji, logger }) => {
  return (
    <div style={styles.wrapper}>
      <button onClick={e => logger(emoji)}>-</button>
      <h4>
        {_name} {emoji}
      </h4>
    </div>
  );
};
about 4 years ago ยท Santiago Trujillo 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!