React JSX error trying to use Less Than Symbol

Viewed 7843

I am trying to make a simple button with an arrow point to the left, so I figured I would use the "<". However when I do

<button><</button>

am I getting an unexpected token error I guess because it thinks I am starting a new element rather than just wanting the < to be the text in the button. Is there anyway around this? Replacing < with "test" compiles and works fine.

3 Answers

HTML entities work; alternatively, you can use braces to interpolate that string in plain JS:

<button>{"<"}</button>

It’s worth noting that in cases where the button text doesn’t tell the user what it does, you should include an aria-label attribute with a description:

<button aria-label="back">{"<"}</button>

This makes your site more accessible by allowing folks with screen readers or similar to easily understand how to interact with it.

Related