Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在类中使用If-else。_Javascript_Reactjs - Fatal编程技术网

Javascript 在类中使用If-else。

Javascript 在类中使用If-else。,javascript,reactjs,Javascript,Reactjs,我们可以在Javascript类中使用if-else语句吗 我在有状态的课堂上学习 在课堂上,我做了类似的事情 import React, { Component } from 'react'; import Aux from '../../HOC/Aux.js'; import Burger from '../../burger/burger.js' import BuildControls from '../../burger/Build-Control/build-controls.js'

我们可以在Javascript类中使用
if-else语句吗

我在有状态的课堂上学习

在课堂上,我做了类似的事情

import React, { Component } from 'react';
import Aux from '../../HOC/Aux.js';
import Burger from '../../burger/burger.js'
import BuildControls from '../../burger/Build-Control/build-controls.js';
class BurgerBuilder extends Component {
state = {
    ingredient: {
      salad: 0,
      bacon: 0,
      cheese: 0,
      meat: 0
    },
    totalPrice: 4,
    purchaseable: false
  }

//Something 

if (this.state.totalPrice != 4) {
  let newState = {
    ...this.state.ingredient
  }
  this.setState({ingredient: newState})
}
但后来它开始抛出这个错误

Unexpected token (63:4)

  61 | 
  62 | 
> 63 | if (this.state.totalPrice != 4) {
     |     ^
  64 |   let newState = {
  65 |     ...this.state.ingredient
  66 |   }

如果我们可以使用If-else语句,那么您知道这为什么不起作用吗?

您可以将该逻辑移动到一个方法
setIngredientState
,并从
构造函数中调用它

代码:


您可以将该逻辑移动到方法
setIngredientState
,并从
构造函数中调用它

代码:


使用组件时,应在函数中使用if语句

您可以在安装组件时执行此操作,也可以在组件更新时执行此操作,如下所示:

*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
render() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
当组件呈现如下方式时,也可以设置if语句:

*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
render() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

另请注意,您不应该使用
=在Javascript中比较值时,最好使用
==在处理组件时,应该在函数中使用if语句

您可以在安装组件时执行此操作,也可以在组件更新时执行此操作,如下所示:

*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
render() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
当组件呈现如下方式时,也可以设置if语句:

*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}
render() {
  if (this.state.totalPrice != 4) {
    let newState = {
      ...this.state.ingredient
    }
    this.setState({ingredient: newState})
  }
}

另请注意,您不应该使用
=在Javascript中比较值时,最好使用
==
if-else语句
应该位于
函数中或
构造函数内部
至少是这样。否。如果需要条件,请在函数中执行。
If-else语句
应在
函数中
或在
构造函数中
至少。否。如果需要条件,请在函数中执行。