1
const ModalButton = ({ label, mode }) => {
  return (
    <button type="submit">
      {mode == "new" && (
        <FaPlusCircle style={{ fontSize: "20px", paddingTop: "6px" }} />) label
      }
    </button>
  );
};

This label is underlined as red in my code editor. Why it won't let me to have JavaScript with JSX?

4
  • 2
    This is invalid syntax, what are you trying to do? Commented Sep 6, 2022 at 10:19
  • What does label contains? Commented Sep 6, 2022 at 10:20
  • I'm trying to conditional render an icon if mode is new render this else another one. Label is a prop (string value) I want to have it next to the icon Commented Sep 6, 2022 at 10:21
  • At least related: stackoverflow.com/questions/69011851/…, stackoverflow.com/questions/48886726/… Commented Sep 6, 2022 at 10:25

3 Answers 3

3

The second operand of && has to be a single value; right now, you're trying to give it two. To treat the icon and the text node after it as a unit (a single value), you need to wrap them in something, such as a fragment: <>_____</>

const ModalButton = ({ label, mode }) => {
    return (
        <button type="submit">
            {mode == "new" && (
                <>
                    <FaPlusCircle style={{ fontSize: "20px", paddingTop: "6px" }} />
                    {label}
                </>
            )}
        </button>
    );
};

(You also had the ) in the wrong place; I've fixed it above. It shouldn't be just after the icon, it should be after the entire operand for && [so with this change, it's after the end of the fragment].)

Also, if you want to use the value of label, you'll need to wrap it in a JSX expression (as above): {label}.

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

3 Comments

This shows me an error to before the label ')' showing in red color
@MishenThakshana - Ah, you weren't missing the ), it was just in the wrong place. I've updated the answer.
@MishenThakshana - Ah yes, of course.
2

As rightly commented by @Konrad Linkowski

This is invalid syntax

What is the issue with it?

What you are using is called expression. So condition1 && condition2 will return false if condition1 is falsey or will return condition2.

In your expression, its condition1 && condition2 condition3. There needs to be an operator between condition2 and condition3 or they need correct structure.


If you want label to always render and icon for "new" buttons, it should be:

{condition1 && condition2}
{condition3}

but if you need them to render together or none, you need to wrap then so that your expression returns 1 value:

{
    condition1 &&
    (<>
        element1
        element2
    </>)
}


Sample

const ModalButton1 = ({ label, mode }) => {
  return (
    <button type="submit">
      {
        mode == "new" && (
        <FaPlusCircle style={{ fontSize: "20px", paddingTop: "6px" }} />)
      }
      { label }
    </button>
  );
};


const ModalButton2 = ({ label, mode }) => {
  return (
    <button type="submit">
      {
        mode == "new" && (
          <>
            <FaPlusCircle style={{ fontSize: "20px", paddingTop: "6px" }} />)
            {label}
          </>
      }
    </button>
  );
};

Comments

0

A JSX expression can only return a single value. If you want to return several values, wrap them in an array, like so:

{mode == "new" && [<Component/>, label]}

3 Comments

I think you might be being downvoted because "A JSX expression can only return a single element" is slightly misleading, it can only return a single value, but as you have shown, that value can contain multiple elements (in the case of arrays/nested JSX elements) Might be worth clarifying.
Or possibly having a component with no key in an array (but if it were me, I'd just comment that, not downvote).
@DBS you're right, edited my answer.

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.