You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React出现Unexpected token语法错误求助:期望逗号,意外标记报错

Understanding the "Unexpected token, expected ," Error in Your React Code

Hey there! Let's break down this confusing error and figure out what's going on.

First, let's decode the error message: "Unexpected token, expected ," (22:4) means that when the JavaScript parser was reading your code at line 22, character 4, it encountered a symbol it didn't expect (in this case, the )}). Instead, it was expecting a comma ( ,) because that's what the current code structure should have required at that point.

Why This Is Happening

Looking at your code snippet:

20 | </div>
21 | </div>
> 22 | )}
| ^
23 | </section>
24 | );
25 | }

This )} on line 22 is almost certainly a misplaced closing for a JavaScript expression inside your JSX (like a conditional render or array map call). Here's the most common scenario:

You probably wrote something like a conditional render using &&, and accidentally closed the expression too early, before your outer <section> tag. For example, if your code was supposed to look like this:

function MyComponent() {
  return (
    <section>
      {isSomeConditionTrue && (
        <div>
          <div>
            {/* Your content here */}
          </div>
        </div>
      )} {/* This )} correctly closes the conditional expression */}
    </section>
  );
}

But instead, you placed the )} right after the inner </div></div>, leaving the </section> hanging outside the valid JSX structure. The parser sees the ) on line 22 and thinks you've closed the return() statement early, so when it hits </section> next, it has no idea what to do—hence the "unexpected token" error.

How to Fix It

  • Trace the matching brackets: Look back in your code to find where the ( that pairs with the ) on line 22 starts. It should be inside a { } block (like {condition && (). Make sure this entire expression is nested inside your <section> tag, so the )} comes before </section>.
  • Check your return statement boundaries: The ( after return should wrap your entire JSX output. That means <section>...</section> must sit fully between that opening ( and the closing ); on line 24.
  • Format your code: Use a formatter like Prettier (most IDEs have this built-in) to auto-indent your code. Misplaced brackets or tags will stand out immediately when the indentation looks wrong.

Don't worry—this kind of mix-up is super common when you're getting used to JSX's mix of HTML-like syntax and JavaScript. Once you trace those brackets, it'll click right into place!

内容的提问来源于stack exchange,提问作者zipfstein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:52:14