简体   繁体   English

React-期望赋值或函数调用,而是看到表达式无法编译

[英]React - Expected an assignment or function call and instead saw an expression failed to compile

So I have this TODO List App with laravel backend and React and when I nom start my React App it shows me this: 所以我有一个带有laravel后端和React的TODO List App,当我启动我的React App时,它向我展示了这一点:

./src/List.js
  Line 126:  Expected an assignment or function call and instead saw an expression  no-unused-expressions

Search for the keywords to learn more about each error.

Anyone have idea why is this happening? 有人知道为什么会这样吗? I tried everything but nothing happens, and I'm new to React, I followed tutorial for this with Laravel API routes and React js frontend 我尝试了一切,但没有任何反应,而且我是React的新手,我通过Laravel API路由和React js前端遵循了该教程

Here is my List.js : 这是我的List.js

import React, { Component } from "react";
import { getList, addItem, deleteItem, updateItem } from "./ListFunctions";

class List extends Component {
  constructor() {
    super();
    this.state({
      id: "",
      title: "",
      editDisabled: false,
      items: []
    });
    this.onSubmit = this.onSubmit.bind(this);
    this.onChange = this.onChange.bind(this);
  }
  componentDidMount() {
    this.getAll();
  }
  onChange = e => {
    this.setState({
      [e.target.name]: e.target.value
    });
  };
  getAll = () => {
    getList().then(data => {
      this.setState(
        {
          title: "",
          items: [...data]
        },
        () => {
          console.log(this.state.items);
        }
      );
    });
  };

  onSubmit = e => {
    e.preventDefault();
    addItem(this.state.title).then(() => {
      this.getAll();
    });
    this.setState({
      title: ""
    });
  };

  onUpdate = e => {
    e.preventDefault();
    addItem(this.state.title, this.state.id).then(() => {
      this.getAll();
    });
    this.setState({
      title: "",
      editDisabled: ""
    });
    this.getAll();
  };

  onEdit = (itemid, e) => {
    e.preventDefault();

    var data = [...this.state.items];
    data.forEach((item, index) => {
      if (item._id === itemid) {
        this.setState({
          id: item.id,
          title: item.title,
          editDisabled: true
        });
      }
    });
  };

  onDelete = (val, e) => {
    e.preventDefault();
    deleteItem(val);
    this.getAll();
  };

  render() {
    return (
      <div className="col-md-12">
        <form onSubmit={this.onSubmit}>
          <div className="form-group">
            <label htmlFor="title">Title</label>
            <div className="row">
              <div className="col-md-12">
                <input
                  type="text"
                  className="form-control"
                  id="title"
                  name="title"
                  value={this.state.title || ""}
                  onChange={this.onChange.bind(this)}
                />
              </div>
            </div>
          </div>
          {!this.state.editDisabled ? (
            <button
              type="submit"
              className="btn btn-success btn-block"
              onClick={this.onSubmit.bind(this)}
            >
              Submit
            </button>
          ) : (
            ""
          )}
          {this.state.editDisabled ? (
            <button
              type="submit"
              onClick={this.onUpdate.bind(this)}
              className="btn btn-primary btn-block"
            >
              Update
            </button>
          ) : (
            ""
          )}
        </form>
        <table className="table">
          <tbody>
            {this.state.items.map((item, index) => {
              <tr key={index}>
                <td className="text-left">{item.title}</td>
                <td className="text-right">
                  <button
                    href=""
                    className="btn btn-info mr-1"
                    disabled={this.state.editDisabled}
                    onClick={this.onEdit.bind(this, item.id)}
                  >
                    Edit
                  </button>
                  <button
                    href=""
                    className="btn btn-danger"
                    disabled={this.state.editDisabled}
                    onClick={this.onEdit.bind(this, item.id)}
                  >
                    Delete
                  </button>
                </td>
              </tr>;
            })}
          </tbody>
        </table>
      </div>
    );
  }
}

export default List;

And Line 126: is the line after Line 126:

          <tbody>
            {this.state.items.map((item, index) => {

That is line 126. Please help 那是第126行。请帮助

You need to explicitly return your jsx 您需要显式return您的jsx

      {this.state.items.map((item, index) => {
          return(
           <tr key={index}>
            <td className="text-left">{item.title}</td>
            <td className="text-right">
              <button
                href=""
                className="btn btn-info mr-1"
                disabled={this.state.editDisabled}
                onClick={this.onEdit.bind(this, item.id)}
              >
                Edit
              </button>
              <button
                href=""
                className="btn btn-danger"
                disabled={this.state.editDisabled}
                onClick={this.onEdit.bind(this, item.id)}
              >
                Delete
              </button>
            </td>
          </tr>);
        })}

Also you're not binding state correctly. 另外,您没有正确binding state

constructor(){
   this.state = {
      id: "",
      title: "",
      editDisabled: false,
      items: []
 }
}

You can also leave bind as long as you use arrow functions for method declaration like onEdit = event => { and not function onEdit(event) . 只要您使用箭头函数进行方法声明(例如onEdit = event => {而不是function onEdit(event)就可以保留bind Just use the following pattern instead: 只需使用以下模式即可:

  onEdit = event => {
     ...
     const { target: { id } } = event;
     this.setState({ id });
  }

  render(){
    return (
      ...
      <button id={item.id} onClick={this.onEdit}>
      ...
     )
   }

You are not returning anything on the map function: 您不会在map函数上返回任何内容:

{
  this.state.items.map((item, index) => {
    return (
      <tr key={index}>
        <td className="text-left">{item.title}</td>
        <td className="text-right">
          <button
            href=""
            className="btn btn-info mr-1"
            disabled={this.state.editDisabled}
            onClick={this.onEdit.bind(this, item.id)}
          >
            Edit
          </button>
          <button
            href=""
            className="btn btn-danger"
            disabled={this.state.editDisabled}
            onClick={this.onEdit.bind(this, item.id)}
          >
            Delete
          </button>
        </td>
      </tr>
    );
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应问题 - 预期分配或 function 调用,而是看到一个表达式 - React issue - Expected an assignment or function call and instead saw an expression 2022 React - 期望一个赋值或 function 调用,而是看到一个表达式 - 2022 React - Expected an assignment or function call and instead saw an expression React 期望一个赋值或函数调用,而是看到一个表达式 - React Expected an assignment or function call and instead saw an expression 期望一个赋值或函数调用,而是看到一个表达式反应路由器 - Expected an assignment or function call and instead saw an expression react router 反应错误 [期望赋值或函数调用,而是看到一个表达式] - React error [Expected an assignment or function call and instead saw an expression] 期望一个赋值或函数调用,而是看到一个表达式做出反应 - expected an assignment or function call and instead saw an expression react 期望分配或 function 调用,而是看到表达式 React JS - Expected an assignment or function call and instead saw an expression React JS React:需要一个赋值或函数调用,而是看到一个表达式 - React : Expected an assignment or function call and instead saw an expression 需要一个赋值或函数调用,而是在React中看到一个表达式 - Expected an assignment or function call and instead saw an expression in React 期望分配或 function 调用,而是看到一个表达式 - React - Expected an assignment or function call and instead saw an expression - React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM