【问题标题】:Getting the error that Expected an assignment or function call and instead saw an expression no-unused-expressions?得到期望赋值或函数调用的错误,而是看到一个表达式 no-unused-expressions?
【发布时间】:2020-06-04 09:52:19
【问题描述】:

我是 react js 新手,我正在学习 Udemy 课程,但我收到错误消息“预期分配或函数调用,而是看到一个表达式 no-unused-expressions”。基本上我使用的是 firebase 数据库在这个应用程序中开发这个应用程序用户把他们的名字作为学生。

import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';

var firebase=require('firebase');
var uuid=require('uuid');

var firebaseConfig = {
    apiKey: "AIzaSyAwXf9-4PgY0ttsGy1FHmyLHZOFO1oVcic",
    authDomain: "uservey-206d4.firebaseapp.com",
    databaseURL: "https://uservey-206d4.firebaseio.com",
    projectId: "uservey-206d4",
    storageBucket: "uservey-206d4.appspot.com",
    messagingSenderId: "723372497898",
    appId: "1:723372497898:web:0ebb13dcb9ce6d2f6eb093",
    measurementId: "G-J6190VVVQH"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

class Survey extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            uid:uuid.v1(),
            studentName:'',
            answers:{
                answer1:'',
                answer2:'',
                answer3:'',
            },
            isSubmitted:false
         };
    }
    render() {
       var studentName;
       var questions;
         if(this.state.studentName ==='' && this.state.isSubmitted === false){
          <div>
              <h1>Please Enter Your Name:</h1>
             <input type="text" className="form-control" placeholder="Please Enter Your Name"/>
              </div>
         }
        return( 
        <div>
            {studentName}
            ------------------------------
            {questions}
            </div> );}
}

export default Survey;

【问题讨论】:

  • 您能否具体说明错误与哪一行有关?

标签: javascript reactjs react-native


【解决方案1】:

render 函数期望你返回 jsx 来渲染,所以:

 render() {
   if(this.state.studentName ==='' && this.state.isSubmitted === false){
     return (
       <div>
         <h1>Please Enter Your Name:</h1>
         <input type="text" className="form-control" placeholder="Please Enter Your Name"/>
       </div>
    )
   }

   return ( 
     <div>
            {studentName}
            ------------------------------
            {questions}
     </div>
   );
}

另外,修正你的缩进,从长远来看,它有助于保持你的理智

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2021-08-23
    • 2019-08-15
    • 2020-12-10
    • 2020-04-21
    • 2019-11-23
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多