You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

React组件返回JSX时报错:Expected an assignment or function call

Fixing the "Expected an assignment or function call..." Error in Your React Form Component

Hey there, let's tackle this error you're seeing with your React form component. That "Expected an assignment or function call and instead saw an expression" message is usually tied to how your JSX is being parsed, or a linting rule flagging something it doesn't recognize as valid. Here are the most common fixes to get your form up and running:

1. Tweak Your JSX Return Syntax

Looking at your code, you're already using parentheses to wrap your JSX (which is the right move for multi-line returns), but sometimes small formatting details can throw off the parser. Try moving the opening parenthesis directly next to the return keyword, and add a semicolon at the end of the closing parenthesis—this follows standard JS syntax and helps the parser correctly identify your JSX block:

import React, { Component } from "react";
class App extends Component {
  render() {
    return (
      <div className="container"> 
        <form> 
          <label>First Name</label> 
          <input type="text" name="firstname" placeholder="Your name.." /> 
          <label>Last Name</label> 
          <input type="text" name="lastname" placeholder="Your last name.." /> 
          <label>Subject</label> 
          <textarea name="subject" placeholder="Write something.."></textarea> 
          <input type="button" value="Submit" /> 
        </form> 
      </div>
    );
  } 
}
export default App;

2. Adjust Your ESLint Configuration (If You're Using It)

This error often comes from ESLint's no-unused-expressions rule, which doesn't automatically recognize JSX as a valid expression (since JSX compiles to React.createElement() calls behind the scenes). To fix this:

  • First, install the ESLint React plugin if you haven't already:
npm install eslint-plugin-react --save-dev
  • Then update your .eslintrc or .eslintrc.json file to enable React-specific rules and either disable the problematic rule or configure it to accept JSX:
{
  "plugins": ["react"],
  "rules": {
    // Disable the generic rule that's flagging JSX
    "no-unused-expressions": "off",
    // Enable essential React rules to help lint your JSX properly
    "react/jsx-uses-react": "error",
    "react/jsx-uses-vars": "error"
  },
  "settings": {
    "react": {
      "version": "detect" // Automatically detect your React version
    }
  }
}

3. Ensure Your Project Can Parse JSX (For Non-CRA Projects)

If you're not using Create React App (which handles JSX parsing out of the box), you need to make sure Babel is set up to process JSX. Here's how:

  • Install the Babel React preset:
npm install @babel/preset-react --save-dev
  • Add it to your Babel config file (like babel.config.json or .babelrc):
{
  "presets": ["@babel/preset-react"]
}

After making one of these changes, your component should render the form without throwing that error. Give it a test run!

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

火山引擎 最新活动