0

please check out my code first.

import styles from './calculator.module.css';

const BUTTONS = [
  {
    key: '+/-',
    styled: 'symbol',
  },
  {
    key: '0',
  },
  {
    key: '.',
    styled: 'symbol',
  },
  {
    key: '=',
    styled: 'equal',
  },
];

const Calculator = () => {
  return (
    <div className={styles.calculator}>
      <section className={styles.buttonGrid}>
        {BUTTONS.map((btn, i) => {
          return (
            <button
              type="button"
              className={`${styles.buttons} ${styles.btn.styled}`} //here is the problem.
              key={`${btn.key}_${i}`}
            >
              {btn.key}
            </button>
          );
        })}
      </section>
    </div>
  );
};

export default Calculator;

Here, I want to use map method and give another classname to style specific components. However, it seems doesn't work.

I tried this way also.

        {BUTTONS.map((btn, i) => {
         
          const customStyle = btn.styled

          return (
            <button
              type="button"
              className={`${styles.buttons} ${styles.customStyle}`}
              key={`${btn.key}_${i}`}
            >
              {btn.key}
            </button>
          );
        })}

and It didn't work well. I have no idea how to solve this problem. How can I get through this?

4
  • use ${styles[btn.styled]} Commented Sep 17, 2022 at 6:55
  • How do I ask a good question?: "Introduce the problem before you post any code" Commented Sep 17, 2022 at 6:56
  • @Andreas My bad. I was in a hurry. Next time I will keep that in mind. Commented Sep 17, 2022 at 6:57
  • How do I ask a good question?: "Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question." Commented Sep 17, 2022 at 7:01

1 Answer 1

1

One of the possible soulutions was this.

className={`${styles.buttons} ${styles[btn.styled]}`}

However, if you try use this format,

const BUTTONS = [
  {
    key: '+/-',
    styled: 'symbol',
  },
  {
    key: '0',
    styled: ''
  },
  {
    key: '.',
    styled: 'symbol',
  },
  {
    key: '=',
    styled: 'equal',
  },
];

You have to add empty string K:V format in the empty ones.

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

Comments

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.